Magento, Magento 2

How to Use Extension Attributes in Magento 2

How to Use Extension Attributes in Magento 2

In this tutorial, Today I will explain how to use extension attributes in Magento 2. Extension attributes are used to allow customize of the service contract. It will helpful to add additional data to the entities. For that, Every interface that extends \Magento\Framework\Api\ExtensibleDataInterface can be extended with the help of the extension attributes. In extension attributes, getExtensionAttributes method returns an auto-generated interface which attribute codes specified in extension_attributes.xml.

You may also like this :

Steps of add extension attributes in quote address :

1) First of all, Let’s assume that you have created simple module in Magento 2. After that, create extension_attributes.xml file at app/code/RH/Helloworld/etc/ and paste the below code to add extension attributes :

<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
        <attribute code="rhphone" type="string" />
    </extension_attributes>
</config>

Let’s see that attributes meaning in short details :

  • for : Add here interface class name in which you want to add extension attribute.
  • code : The name of the attribute code.
  • type : The data type of the attribute code. The data type can be string, int, array or complex type such as an interface.

Here, I added rhphone attribute for extension attribute. It will use for shipping address. For that, I will create one plugin and get value of that attribute.

2) Secondly, create di.xml for initialize plugin at app/code/RH/Helloworld/etc/ and paste the below code :

<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Model\ShippingInformationManagement">
        <plugin name="rh_extension_attribute"
                type="RH\Helloworld\Plugin\Checkout\Model\ShippingInformationManagement"
                sortOrder="1" />
    </type>
</config>

3) After that, Create ShippingInformationManagement.php file at app/code/RH/Helloworld/Plugin/Checkout/Model/ and paste the below code to get and set rhphone attribute value :

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\Helloworld\Plugin\Checkout\Model;
 
use Magento\Quote\Model\QuoteRepository;
use Magento\Checkout\Model\ShippingInformationManagement;
use Magento\Checkout\Api\Data\ShippingInformationInterface;

class ShippingInformationManagement
{
    /**
     * Set rhphone Attribute Value
     * 
     * @param  ShippingInformationManagement $subject
     * @param                                $cartId
     * @param  ShippingInformationInterface  $addressInformation
     */
    public function beforeSaveAddressInformation(
        ShippingInformationManagement $subject,
        $cartId,
        ShippingInformationInterface $addressInformation
    ) {
        $shippingAddress = $addressInformation->getShippingAddress();
        $shippingAddressExtensionAttributes = $shippingAddress->getExtensionAttributes();
        if ($shippingAddressExtensionAttributes) {
            $rhPhone = $shippingAddressExtensionAttributes->getRhphone();
            $shippingAddress->setRhphone($rhPhone);
        }
    }
}

Note : rhphone field should be available in quote_address table.

Now, you can check rhphone value in shipping address. You will get rhphone attribute value in that shipping address object.

That’s it !!!

I hope this blog is easy to understand about how to use extension attributes in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Keep liking and sharing !!

Tagged ,