Skip to content

๐Ÿ“ Community Note The content on this page was generated with the assistance of AI and is pending a human review. While we've done our best to ensure accuracy, there may be discrepancies or areas that could be improved.

extension_attributes.xml Reference Documentation

The extension_attributes.xml file is an essential part of Magento 2, providing a robust way to extend Magento service contracts' data. Extension attributes are custom attributes that are added to entities such as products, orders, customers, etc.

Overview

The extension_attributes.xml file is typically located under the etc directory in a module:

app
โ””โ”€โ”€ code
โ””โ”€โ”€ Vendor
โ””โ”€โ”€ Module
โ””โ”€โ”€ etc
โ”œโ”€โ”€ extension_attributes.xml
โ””โ”€โ”€ ...

File Structure

The structure of the extension_attributes.xml file in Magento 2 follows this pattern:

<?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\Catalog\Api\Data\ProductInterface">
<attribute code="custom_attribute" type="string"/>
</extension_attributes>
</config>
  • for specifies the interface for which the extension attribute is added.
  • code is the unique name for the extension attribute.
  • type is the data type for the extension attribute.

Defining Extension Attributes

Defining an extension attribute requires specifying the for attribute, which mentions the Interface against which we are defining the attribute. The code and type define the attribute itself.

Here is an example of adding a custom attribute to the Product entity:

<extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
<attribute code="custom_attribute" type="string"/>
</extension_attributes>

Accessing Extension Attributes

Extension attributes can be accessed and set via the entity's extension attributes object. Here's an example using the product entity:

<?php
$product = $this->productRepository->get('product_sku');
$extensionAttributes = $product->getExtensionAttributes();
 
// Get the value of the custom_attribute
$customAttributeValue = $extensionAttributes->getCustomAttribute();
 
// Set a value for the custom_attribute
$extensionAttributes->setCustomAttribute('New Value');
$product->setExtensionAttributes($extensionAttributes);
 
$this->productRepository->save($product);

Best Practices

  1. Extension attributes should be added for service contract interfaces only. Do not use them for other classes or interfaces.

  2. It's best not to use extension attributes as a substitute for custom attributes. Use them when you need to add data to an entity that does not naturally belong to it.

  3. Keep in mind that extension attributes are not persisted automatically. Developers need to handle the persistence ( saving/loading) of these attributes manually.

Conclusion

The extension_attributes.xml file is a powerful tool in Magento 2, enabling developers to extend data for service contract interfaces. Proper understanding and usage of extension attributes contribute to building flexible and extendable custom modules.