๐ 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
- Overview
- File Structure
- Defining Extension Attributes
- Accessing Extension Attributes
- Best Practices
- Conclusion
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
-
Extension attributes should be added for service contract interfaces only. Do not use them for other classes or interfaces.
-
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.
-
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.