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.
I have used \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory in my function to fetch collection.
I have created custom data interface named CustomProductInterface to return my custom data.
Basically I am not returning \Magento\Catalog\Api\Data\ProductInterface and instead returning my CustomProductInterface.
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.