{"id":950,"date":"2020-04-21T12:24:51","date_gmt":"2020-04-21T12:24:51","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/04\/21\/add-extension-attribute-to-entity-in-magento-2\/"},"modified":"2025-05-22T13:27:18","modified_gmt":"2025-05-22T07:57:18","slug":"add-extension-attribute-to-entity-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-extension-attribute-to-entity-in-magento-2\/","title":{"rendered":"How To Add Extension Attribute to Entity In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In Magento 2, extension attributes extend the default functionality and generally use more complex data types than the custom attributes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Us developers cannot change the API data interfaces defined in the default Magento. But these entities have the extension attributes feature.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They are the additional data that we want to add in our entity. And the post gives the solution to&nbsp;<em><strong>add extension attribute to entity in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Extension Attributes are used to allow for customization of the strict Service Contracts.&nbsp;These attributes do not appear on the GUI.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method To Add Extension Attribute to Entity In Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>app\/code\/Vendor\/Extension\/etc\/extension_attributes.xml<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config>\n    &lt;extension_attributes for=\"Path\\To\\Interface\">\n        &lt;attribute code=\"name_of_attribute\" type=\"datatype\">\n        &lt;\/attribute>\n    &lt;\/extension_attributes>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example: Create&nbsp;<em><strong>app\/code\/Vendor\/Extension\/etc\/extension_attributes.xml<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Api\/etc\/extension_attributes.xsd\">\n    &lt;extension_attributes for=\"Magento\\Quote\\Api\\Data\\AddressInterface\">\n        &lt;attribute code=\"attribute_name\" type=\"string\"\/>\n    &lt;\/extension_attributes>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution on social media for helping the fellow developers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-950","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=950"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/950\/revisions"}],"predecessor-version":[{"id":14955,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/950\/revisions\/14955"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}