1

Hello i have to add extension attributes to Magento\Catalog\Api\Data\ProductInterface

So i added this one to extension_attrributes.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
        <attribute code="custom_attribute" type="int"/>
    </extension_attributes>
</config>

And to di.xml added:

<type name="Magento\Framework\EntityManager\Operation\ExtensionPool">
        <arguments>
            <argument name="extensionActions" xsi:type="array">
                <item name="Magento\Catalog\Api\Data\ProductInterface" xsi:type="array">
                    <item name="read" xsi:type="array">
                        <item name="customAttributeReader" xsi:type="string">Vendor\Custom\Model\Product\CustomAttribute\ReadHandler</item>
                    </item>
                    <item name="create" xsi:type="array">
                        <item name="customAttributeCreator" xsi:type="string">Vendor\Custom\Model\Product\CustomAttribute\SaveHandler</item>

                    </item>
                    <item name="update" xsi:type="array">
                        <item name="customAttributeSaver" xsi:type="string">Vendor\Custom\Model\Product\CustomAttribute\SaveHandler</item>
                    </item>
                </item>
            </argument>
        </arguments>
    </type>

So it`s work fine. When i get product entity using ProductRepository or load() function in Product model, all my extension attributes loads successful. But i have some issue when i work with Collection:

When i do something like:

$productCollection->addFieldToFilter('custom_attribute', 2);

My code don't wok and it's expected because my model not contains this attribute.

So i have a question. How i can filter by my extension attribute using collection ?

Also if i do:

$productCollection->getFirstItem()->getExtensionAttributes()

I get empty array.

So i need your help. I'll be very grateful!!!

Your Answer

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

Browse other questions tagged or ask your own question.