2

Error : Uncaught Error: Call to undefined method Magento\Catalog\Api\Data\ProductExtension: : setDummyAttribute() on ProductExtension in MyCompany\MyModule\Plugin\DummyAttribute"

This is how I have implemented extension attribute.

  1. I have used \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory in my function to fetch collection.

  2. I have created custom data interface named CustomProductInterface to return my custom data.

  3. Basically I am not returning \Magento\Catalog\Api\Data\ProductInterface and instead returning my CustomProductInterface.

  4. Everything is working fine so far. Now I am creating extension attribute on CustomProductInterface. After adding this attribute, it starts throwing exception as "call to undefined method getDummyAttribute() on ProductExtension"

After debugging a lot, I found that CustomProductInterface is internally called as interface and corresponding object called is of Magento\Catalog\Api\Data\ProductExtension and not MyCompany\MyModule\Api\Data\CustomProductExtension.

<extension_attributes for="MyCompany\MyModule\Api\Data\CustomProductInterface">
    <attribute code="dummy_attributte" type="string"/>
</extension_attributes>

It works fine If I set extension_attribute for both the Interfaces as mentioned below:

<extension_attributes for="MyCompany\MyModule\Api\Data\CustomProductInterface">
    <attribute code="dummy_attributte" type="string"/>
</extension_attributes>

<extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
    <attribute code="dummy_attributte" type="string"/>
</extension_attributes>

My requirement is such that I have to use product collection with joins on my custom tables and display required data using my custom interface.

So I have found the solution, but it seems improper to me and I am looking for explanation of above case.

According to my research, it seems like we cannot use extension attribute on custom collection.

  • did you run bin/magento setup:di:compile ? – Marius Feb 3 at 11:27
  • @Marius : Yes, I have checked and not getting any compilation issues. – Sanjay Chaudhary Feb 3 at 11:32
1

I guess I just found the answer. You just have to iterate the core product collection and set the data in your custom interface.

Non-working old code/buggy code :

public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
    {
       $collection = $this->productCollectionFactory->create();
      /*Join custom data with main/core product collection. */
       $collection = $this->joinExtraTablesForCustomData($collection);
       $searchResult->setSearchCriteria($searchCriteria);
       $searchResult->setItems($collection->getItems());
       $searchResult->setTotalCount($collection->getSize());
       return $searchResult;
    }

The problem in above code is $collection->getItems() will have an array objects of type \Magento\Catalog\Api\Data\ProductInterface. So we have to explicitly change the type to Custom Interface as mentioned below.

This works perfectly fine.

public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
{
   $collection = $this->productCollectionFactory->create();
  /*Join custom data with main/core product collection. */
   $collection = $this->joinExtraTablesForCustomData($collection);
   foreach ($collection as $item)
   {
       $customProduct = $this->customProductInterfaceFactory->create();
       $customProduct->setData($item->getData());
       $customProducts[] = $customProduct;
   }
   $searchResult->setSearchCriteria($searchCriteria);
   $searchResult->setItems($customProducts);
   $searchResult->setTotalCount($collection->getSize());
   return $searchResult;
}
| 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.