How to Add Magento 2 Extension Attributes

7 Min | September 30, 2024

Magento 2 is a powerful ecommerce platform, but sometimes the default functionalities might not meet your business’s demands. In such cases, third-party modules and customizations become essential to extend Magento 2’s capabilities. One effective way to enhance the functionality of Magento 2 entities is by using extension attributes.

Extension attributes allow you to add additional complex data to an existing entity class without modifying the core code, making them invaluable for developers who need to customize Magento 2 for unique business requirements. This guide will walk you through the steps to add extension attributes to an entity in Magento 2.

Understanding Extension Attributes in Magento 2

Before proceeding, it’s essential to understand extension attributes and their importance. Extension attributes extend the default functionalities of Magento 2 entities by allowing developers to add custom data without altering the core Magento code. This approach is especially useful when dealing with third-party modules, where you cannot modify the core API data interfaces directly.

Steps to Add Extension Attributes

Adding extension attributes to an entity in Magento 2 can be broken down into three main steps:

  1. Retrieve a Product/List of Products from the Magento API
  2. Add Plugin to Product Repository
  3. Configure Extension Attributes

Let’s explore each step in detail.

Retrieve a Product/List of Products from the Magento API

First, you must retrieve the product or list of products from the Magento API. This is done by requesting an API to the appropriate service, typically the Product Repository. The response from the API request will return objects that include structure for both custom attributes and extension attributes.

For example, the structure of a product response might look like this:

<product>
    <id>1</id>
    <sku>some-sku</sku>
    <custom_attributes><!-- Custom Attributes Data --></custom_attributes>
    <extension_attributes><!-- Extension Attributes Data --></extension_attributes>
</product>

Similarly, if you request a list of products, the structure will look like this:

<products>
    <item>
        <id>1</id>
        <sku>some-sku</sku>
        <custom_attributes><!-- Custom Attributes Data --></custom_attributes>
        <extension_attributes><!-- Extension Attributes Data --></extension_attributes>
    </item>
    <item>
        <id>2</id>
        <sku>some-sku-2</sku>
        <custom_attributes><!-- Custom Attributes Data --></custom_attributes>
        <extension_attributes><!-- Extension Attributes Data --></extension_attributes>
    </item>
</products>

This is where you’ll later add your extension attributes.

Add Plugin to Product Repository

You need to use a plugin in the Product Repository to add extension attributes. Plugins allow you to extend or modify the behavior of public methods in Magento 2 classes. In this case, you’ll create an after-plugin that adds your custom data to the extension attributes of the product entity.

Here’s an example of how to create an afterGet plugin:

public function afterGet(
    \Magento\Catalog\Api\ProductRepositoryInterface $subject,
    \Magento\Catalog\Api\Data\ProductInterface $entity
) {
    $ourCustomData = $this->customDataRepository->get($entity->getId());
    $extensionAttributes = $entity->getExtensionAttributes(); 
    $extensionAttributes->setOurCustomData($ourCustomData);
    $entity->setExtensionAttributes($extensionAttributes);
    return $entity;
}

In this plugin:

  • First, the existing extension attributes are retrieved using getExtensionAttributes().
  • Then, your custom data is added to these attributes.
  • Finally, the modified extension attributes are set back to the entity using setExtensionAttributes().

You can create similar plugins for afterGetList and afterSave methods to handle multiple products or save operations.

Configure Extension Attributes

Now that you have set up the plugin, the next step is configuring the extension attributes. This configuration will differ based on whether your attributes are scalar or non-scalar.

Scalar Attributes

Scalar attributes represent simple data types such as strings or integers. Here’s how to configure scalar extension attributes:

<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="first_custom_attribute" type="Magento\SomeModule\Api\Data\CustomDataInterface" />
        <attribute code="second_custom_attribute" type="Magento\SomeModule\Api\Data\CustomDataInterface" />
    </extension_attributes>
</config>

The resulting product structure will include custom attributes like this:

<product>
    <id>1</id>
    <sku>some-sku</sku>
    <custom_attributes><!-- Custom Attributes Data --></custom_attributes>
    <extension_attributes>
        <first_custom_attribute>1</first_custom_attribute>
        <second_custom_attribute>2</second_custom_attribute>
    </extension_attributes>
</product>

Non-scalar Attributes

Non-scalar attributes represent more complex data types, such as arrays or objects. Here’s how to configure non-scalar extension attributes:

<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>

The resulting product structure will look like this:

<product>
    <id>1</id>
    <sku>some-sku</sku>
    <custom_attributes><!-- Custom Attributes Data --></custom_attributes>
    <extension_attributes>
        <our_custom_data>
            <first_custom_attribute>1</first_custom_attribute>
            <second_custom_attribute>2</second_custom_attribute>
        </our_custom_data>
    </extension_attributes>
</product>

Conclusion

Adding extension attributes in Magento 2 is a powerful way to customize and extend the functionality of entities without modifying the core code. By following the steps outlined in this guide—retrieving products from the Magento API, adding a plugin to the Product Repository, and configuring extension attributes—you can successfully implement custom attributes that meet your business needs.

The Author

Anmol is a dedicated technical content writer known for her practical approach. She believes in experiencing processes firsthand before translating them into insightful content. Additionally, she is good at WordPress development and skills of digital forensics and cybersecurity. Beyond her professional endeavors, she enjoys playing sports games, particularly table tennis and badminton, valuing the balance between mental and physical agility.

Scroll to Top