cancel
Showing results for 
Search instead for 
Did you mean: 

How i can work with extension attributes using collection?

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

How i can work with extension attributes using collection?

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!!!

3 REPLIES 3

Re: How i can work with extension attributes using collection?

  @VitaliyMain 

 

I am not sure this is fit for your requirement. but i think you have to go through this.

 

The system uses a join directive to add external attributes to a collection and to make the collection filterable. The join element in the extension_attributes.xml file defines which object fields and the database table/column to use as the source of a search.

 

In the following example, an attribute named stock_item of type Magento\CatalogInventory\Api\Data\StockItemInterface is being added to the Magento\Catalog\Api\Data\ProductInterface.

<extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
    <attribute code="stock_item" type="Magento\CatalogInventory\Api\Data\StockItemInterface">
        <join reference_table="cataloginventory_stock_item" reference_field="product_id" join_on_field="entity_id">
            <field>qty</field>
        </join>
    </attribute>
</extension_attributes>

When getList() is called, it returns a list of ProductInterfaces. When it does this, the code populates the stock_item with a joined operation in which the StockItemInterface’s qty property comes from the cataloginventory_stock_item table where the Product’s entity_Id is joined with the cataloginventory_stock_item.product_id column.  

 

See the detail documentation

 

I hope it helps!

If Issue Solved, Click Kudos/Accept As solutions.

Re: How i can work with extension attributes using collection?

@Patel_Chirag Thank for your answer. 

Using getList() of repository is well and work pretty nice. But i have to get list of ProductInterfaces via Magento\Catalog\Model\ResourceModel\Product\Collection. 

So when i do: 

$productCollection->getItems();

I get products items  but my extension attributes are empty. 

So how i can populate them when i work with collection?

Re: How i can work with extension attributes using collection?

Did you get a solution for above problem? I also want to know if magento is providing anything to achieve this otherwise I think we need to use plugin for that.