Jump to content

about decorates ProductDataProvider on prestashop 1.7


Recommended Posts

hi,i have new service on my module:mymodule/src/Services/ProductDataProviderService.php

i want override src/Adapter/Product/ProductDataProvider.php some function,

so i use https://devdocs.prestashop.com/1.7/modules/concepts/services/#decorate-the-service

 

and i am Chinese,i use google translate the doc,i am not sure i really understand the doc.

 

mymodule/config/services.yml

services:
    jinber.etproductgif.prestashop.adapter.data_provider.product:
        class: Jinber\ProductGif\Services\ProductDataProviderService
        decorates: 'prestashop.adapter.data_provider.product'
        arguments: [ '@jinber.etproductgif.prestashop.adapter.data_provider.product.inner' ]
        public: false

 

mymodule/src/Services/ProductDataProviderService.php

 

<?php

namespace Jinber\ProductGif\Services;
class ProductDataProviderService
{

    public function getImage($id_image)
    {
        $imageData = new \Image((int)$id_image);

        return [
            'id' => $imageData->id,
            'id_product' => $imageData->id_product,
            'position' => $imageData->position,
            'cover' => $imageData->cover ? true : false,
            'legend' => $imageData->legend,
            'format' => 'gif',
            'base_image_url' => _THEME_PROD_DIR_ . $imageData->getImgPath(),
        ];
    }
}

when i refresh page, i get UndefinedMethodException:

 

Attempted to call an undefined method named "getProduct" of class "Jinber\ProductGif\Services\ProductDataProviderService".

so i extends the PrestaShop\PrestaShop\Adapter\Product\ProductDataProvider, now my code like this:

<?php

namespace Jinber\ProductGif\Services;
use PrestaShop\PrestaShop\Adapter\Product\ProductDataProvider;
class ProductDataProviderService extends ProductDataProvider
{

    public function getImage($id_image)
    {
        $imageData = new \Image((int)$id_image);

        return [
            'id' => $imageData->id,
            'id_product' => $imageData->id_product,
            'position' => $imageData->position,
            'cover' => $imageData->cover ? true : false,
            'legend' => $imageData->legend,
            'format' => 'gif',
            'base_image_url' => _THEME_PROD_DIR_ . $imageData->getImgPath(),
        ];
    }
}

 

now it`s working. so i want know extends ProductDataProvider is correct right?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...