Extension attribute error “Call to undefined method Magento\Catalog\Api\Data\ProductExtension: : setDummyAttribute() on ProductExtension”
-
07-04-2021 - |
题
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.
解决方案
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;
}