0

I want to add extra data in product list rest api using extension attribute via Plugin in Magento 2 but it's not working.

> Uncaught TypeError: Argument 2 passed to
> Vendor\ModuleName\Plugin\ProductPlugin::afterGetList() must be an
> instance of Magento\Catalog\Api\Data\ProductSearchResultsInterface,
> instance of Magento\Framework\Api\SearchResults given

extension_attribute.xml

<extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
    <attribute code="seller" type="Magento\Catalog\Api\Data\CustomOptionInterface[]" />
</extension_attributes>

di.xml

<type name="Magento\Catalog\Api\ProductRepositoryInterface">
    <plugin name="add_more_info_in_product" type="Vendor\ModuleName\Plugin\ProductPlugin" sortOrder="15" />
</type>

Vendor\ModuleName\Plugin\ProductPlugin.php

use Magento\Catalog\Api\Data\ProductInterface;

class ProductPlugin
{
    protected $productExtensionFactory;
    protected $productFactory;

    public function __construct(
        \Magento\Catalog\Api\Data\ProductExtensionFactory $productExtensionFactory,
        \Magento\Catalog\Model\ProductFactory $productFactory
    )
    {
        $this->productFactory = $productFactory;
        $this->productExtensionFactory = $productExtensionFactory;
    }

    public function afterGet(
        \Magento\Catalog\Api\ProductRepositoryInterface $subject,
        \Magento\Catalog\Api\Data\ProductInterface $entity
    )
    {
        /** @var ProductInterface $product */
        $product = $entity;
        // Fetch the raw product model (I have not found a better way), and set the data onto our attribute.
        //$productModel = $this->productFactory->create()->load($product->getId());
        $extensionAttributes = $product->getExtensionAttributes(); /** get current extension attributes from product **/
        $seller = [
        'id' => (int) 10,
        'name' => 'seller name',
        'image' => null
    ];
        $extensionAttributes->setSeller($seller);
        $product->setExtensionAttributes($extensionAttributes);
        return $product;
    }

    public function afterGetList(
        \Magento\Catalog\Api\ProductRepositoryInterface $subject,
        \Magento\Catalog\Api\Data\ProductSearchResultsInterface $searchCriteria
    ) : \Magento\Catalog\Api\Data\ProductSearchResultsInterface
    {
        $products = [];
        foreach ($searchCriteria->getItems() as $entity) {
            /** @var ProductInterface $product */
            // Fetch the raw product model (I have not found a better way), and set the data onto our attribute.
            //$productModel = $this->productFactory->create()->load($product->getId());
            $extensionAttributes = $entity->getExtensionAttributes(); /** get current extension attributes from product **/
            $seller = [
        'id' => (int) 10,
        'name' => 'seller name',
        'image' => null
    ];
            $extensionAttributes->setSeller($seller);
            $entity->setExtensionAttributes($extensionAttributes);

            $products[] = $entity;
        }
        $searchCriteria->setItems($products);
        return $searchCriteria;
    }
}

But the result for product list is null:

{
    "status_code": 200,
    "reason_phrase": "OK",
    "body": null
}

So any suggestion about that please?

  • can't figure out your issue with the dummy values in code, most of what you've done is right. Posting your original code would help. – Vivek Kumar Jun 4 at 7:22
  • @VivekKumar i have update my question.thank you – mahmoudismail Jun 4 at 7:28
0

I think your issue is with ;

 $seller = [
        'id' => (int) 10,
        'name' => 'seller name',
        'image' => null
    ];

I don't think there is any attribute type that will let you assign an array. If you do have to key value pairs, please json encode the values to a json string and assign to the extension attributes like $extensionAttributes->setSeller(JSON_STRING); and it should work.

| improve this answer | |
0

We need to pass the instance of ProductSearchResultsInterface, not the interface. \Magento\Framework\Api\SearchResults is the instance of the interface.

Changes plugin:

public function afterGetList(\Magento\Catalog\Api\ProductRepositoryInterface $subject, \Magento\Framework\Api\SearchResults $searchResults) {
    ......
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.