What is extension Attribute in Magento 2?

0
admin
Aug 13, 2021 06:13 PM 1 Answers Magento2
Member Since Jun 2020
Subscribed Subscribe Not subscribe
Flag(0)
0 Subscribers
Submit Answer
Please login to submit answer.
1 Answers
Best Answer
1
rajat104
Aug 13, 2021
Flag(0)

Third-party developers cannot change the API Data interfaces defined in the Magento Core module.  Check the interface for the methods getExtensionAttributes() and setExtensionAttributes() to determine if they are available for the entity.

To add the getter and setter method dynamically to the existing interface without modifying the core code Magento provide the facility to add extension attribute so that your data can expose with the API.

You have to create extension_attributes.xml inside your module etc folder like below

<config xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“urn:magento:framework:Api/etc/extension_attributes.xsd”> <extension_attributes for=“{interface class name where you want to add getter and setter”> <attribute code=“{attribute code}” type=“{data type}” /> </extension_attributes> </config>

Example:

<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="some_custom_data" type="string[]" />
    </extension_attributes>
</config>
<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="our_custom_data" type="Magento\SomeModule\Api\Data\CustomDataInterface" />
    </extension_attributes>
</config>
Sign in to Reply
Replying as Submit