What is the purpose of Extension attributes in Magento 2?

Jul 18th, 2018 4 min to read

This article will throw some light on how one can use the extension attributes in Magento 2. Extension attributes are primarily containers that are used for adding an additional piece of information to our entities.

Every interface that extends \Magento\Framework\Api\ExtensibleDataInterface can be extended with the help of the extension attributes. The methods defined in ExtensibleDataInterface grants access to the objects that contain the additional data.

Below example will give you a clear picture of what we are trying to explain. For this first, we will create one module named DemoModule.

Step 1 – Declare Extension Attributes

We will be declaring the extension attributes in app/code/Aureatelabs/DemoModule/etc/extension_attributes.xml

<?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\Checkout\Api\Data\ShippingInformationInterface">
       <attribute code="custom_shipping_charge" type="string"/>

This means that we are extending our shipping information Interface with an attribute. It’s accessible from $order->getExtensionAttributes()->getCustomShippingAttribute().

Below are short descriptions that will explain what the words stand for:

  • for – The fully-qualified type name with the namespace that processes the extensions. The value must be a type that implements `ExtensibleDataInterface`. The interface can be in a different module.
  • code – The name of the attribute. The attribute name should be in snake case (the first letter in each word should be in lowercase, with each word separated by an underscore). 
  • type – The data type. This can be a simple data type, such as string or integer, or complex type, such as an interface.

Step 2 – Clear Generated Files

Now clear the var/generation when you run setup:di:compile command, new getter and setter methods will be added in /var/generation/Magento/Checkout/Api/Data/ShippingInformationExtensionInterface.php

Step 3 – Set/Get Attributes

Now you can set/get these attributes value by creating the instance of Magento/Checkout/Api/Data/ShippingInformationInterface.php interface.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$addressInformation = $objectManager->create('Magento\Checkout\Api\Data\ShippingInformationInterface');
$extAttributes = $addressInformation->getExtensionAttributes();

$selectedShipping = $extAttributes->getCustomShippingCharge();

One important thing to note, for the Extension Attributes to work as proposed we always need to query the entity for the existing extension_attributes via getExtensionAttributes() before using the extension attribute factory to create a new one. In case if we omit this check any previously set extension attributes on the entity will we wiped off.

One of the benefits that you will get from using Extension Attributes is that your added attribute data is automatically available through an API request.


What are extensions in Magento 2?

Extensions in Magento 2 are additional files or code packages used to add features to the existing interface. These are not part of the core code but can customize the code for additional functionality through features like SEO optimization, performance enhancement, accounts and payments management, data collection and analytics, and other similar functions. Through extensions, a wide variety of customizations is possible from time to time without disturbing the original framework.

What are Active Directory extension attributes?

Active Directory extension attributes are additional spaces that can be added to the Azure directory to save more information. These spaces are utilized for data that isn’t traditionally included in the Azure data scheme such as employee details, job titles, or tenant information. These extensions can be created, managed, modified, or deleted any time by the admin.

What is custom attribute in Magento 2?

Custom attributes in Magento 2 are custom spaces that can be added to any category to describe attributes that aren’t already present in the predefined entities. These can be new product options, modifications, or customer preferences. Custom attributes can be enabled, disabled, or allowed for view only by the admin. Vendors and customers can access only those attributes that are allowed by the admin.

How do I manage attributes in Magento 2?

To manage attributes in Magento 2, follow the below steps:
1. Login to access the admin dashboard.
2. Click on Stores>Attributes>Products. This displays all the current attributes in the store.
3. To add an attribute, click on the “Add New Attribute” button.
4. Use the “Properties” tab to change the title, values, and appearance of the newly added attribute. 5. Click on “Save” to save the properties.

Speak your Mind

Post a Comment

Got a question? Have a feedback? Please feel free to leave your ideas, opinions, and questions in the comments section of our post! ❤️

* This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Grow your online business like 3,142 subscribers

    * This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

    Thank You!

    We are reviewing your submission, and will be in touch shortly.