How To Add Extension Attribute to Entity In Magento 2
In Magento 2, extension attributes extend the default functionality and generally use more complex data types than the custom attributes.
Us developers cannot change the API data interfaces defined in the default Magento. But these entities have the extension attributes feature.
They are the additional data that we want to add in our entity. And the post gives the solution to add extension attribute to entity in Magento 2.
The extensibility of default features is what makes Magento 2 stand apart from other CMSs. However, there is the cost of this flexibility, i.e., stability. The extensibility and stability are two opposite end of the poles and to create a right balance, Service Contracts are introduced.
Extension Attributes are used to allow for customization of the strict Service Contracts. These attributes do not appear on the GUI.
Method To Add Extension Attribute to Entity In Magento 2:
Create app/code/Vendor/Extension/etc/extension_attributes.xml
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <config> <extension_attributes for="Path\To\Interface"> <attribute code="name_of_attribute" type="datatype"> </attribute> </extension_attributes> </config> |
Example: Create app/code/Vendor/Extension/etc/extension_attributes.xml
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd"> <extension_attributes for="Magento\Quote\Api\Data\AddressInterface"> <attribute code="attribute_name" type="string"/> </extension_attributes> </config> |
That’s it.
Any doubts about the topic can be mentioned in the Comments section. I’ll gladly help you out.
Feel free to share the solution on social media for helping the fellow developers.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How To Import Product Data Programmatically In Magento 2
Best Magento 2 Themes in 2024 (Free & Paid)
Next