Back to Top

How to add extension attribute in Magento 2

Updated 5 February 2026

Magento 2 provides extension attribute to extend core API data interfaces safely.
They allow developers to add custom data without modifying core Magento code.

This approach keeps your store upgrade-safe and API-friendly.

Also have you check our Magento 2 Order attributes extension?

What Are Extension Attributes in Magento 2?

Extension attributes allow you to add extra fields to Magento service contracts.
They attach additional data to existing API data interfaces.

Magento exposes these attributes automatically in REST and SOAP APIs.
This makes them ideal for integrations and frontend customizations.

Searching for an experienced
Magento 2 Company ?
Find out More

Why Magento Introduced Extension Attributes

Magento restricts direct modification of data interfaces.
This ensures backward compatibility and stable APIs.

Extension attributes solve this limitation by allowing structured extension.
They support both simple and complex data types.

Extension Attributes vs Custom Attributes

Custom attributes store simple values like text or numbers.
They usually map directly to database columns.

Extension attributes support objects, arrays, and complex structures.
They work best for API-level data enhancements.

When Should You Use Extension Attributes?

Use extension attributes when:

  • You need to extend API responses
  • You want to pass extra data during checkout
  • You want structured or grouped data
  • You must avoid modifying core tables

Practical Scenario: Add Custom Delivery Instructions at Checkout

Assume you want to add Delivery Instructions during checkout.
You want this data available in APIs and order processing.

The default checkout does not support this field.
Extension attributes provide the correct solution.

Step 1: Identify the Target Interface

Magento checkout uses ShippingInformationInterface.
This interface transfers shipping data during checkout.

We will extend this interface with a custom field.
The field will store delivery instructions.

Step 2: Create extension_attributes.xml

Create the file at:

app/code/Vendor/Module/etc/extension_attributes.xml

This file declares new extension attributes for interfaces.

Example: Adding a Scalar Extension Attribute

Scalar attributes store simple values like strings.
Delivery instructions fit this category.

<?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="delivery_instructions" type="string"/>
    </extension_attributes>
</config>

Magento now recognizes this field at the API level.

Step 3: How Magento Generates Getters and Setters

Magento automatically generates methods for extension attributes.
You do not manually create getters or setters.

Magento creates these methods during compilation:

  • getDeliveryInstructions()
  • setDeliveryInstructions($value)

Step 4: Read Extension Attribute

Magento provides access via extension attributes object.
You should never access it directly from request data.

public function beforeSaveAddressInformation(
    \Magento\Checkout\Api\ShippingInformationManagementInterface $subject,
    $cartId,
    \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
) {

    $extAttributes = $addressInformation->getExtensionAttributes();

    if ($extAttributes && method_exists($extAttributes, 'getDeliveryInstructions')) {
        $deliveryInstructions = $extAttributes->getDeliveryInstructions();
        // Your custom code here
    }

    return [$cartId, $addressInformation];
}

This ensures compatibility with Magento APIs.

Step 5: Persist Data to Database

You can store extension attribute data in:

  • Quote table
  • Order table
  • Custom database table

Use repositories or resource models for clean persistence.

Step 6: Non-Scalar Extension Attribute Example

Sometimes you need complex data like multiple delivery options.
Scalar types are not enough in this case.

Magento supports object and array-based extension attributes.

XML for Non-Scalar Attribute

<attribute code="delivery_slots" type="Vendor\Module\Api\Data\DeliverySlotInterface[]"/>

This allows multiple structured values.

Step 7: Create Custom Data Interface

Create the interface:

Vendor/Module/Api/Data/DeliverySlotInterface.php

namespace Vendor\Module\Api\Data;

interface DeliverySlotInterface
{
    const DATE = 'date';
    const TIME = 'time';
    
    /**
     * Get Name
     *
     * @return string|null
     */
    public function getDate();
    
    /**
     * Get Name
     *
     * @return string|null
     */
    public function getTime();

    /**
     * Set Date
     *
     * @param string $date
     * @return $this
     */
    public function setDate($date);
    
    /**
     * Set Time
     *
     * @param string $time
     * @return $this
     */
    public function setTime($time);
}

Now create it implementation model:

Vendor/Module/Model/DeliverySlot.php

<?php
namespace Vendor\Module\Model;

use Vendor\Module\Api\Data\DeliverySlotInterface;
use Magento\Framework\DataObject;

class DeliverySlot extends DataObject implements DeliverySlotInterface
{
    public function getDate()
    {
        return $this->_getData('date');
    }

    public function setDate($date)
    {
        return $this->setData('date', $date);
    }

    public function getTime()
    {
        return $this->_getData('time');
    }

    public function setTime($time)
    {
        return $this->setData('time', $time);
    }
}

Magento now understands the object structure.

Conclusion

Extension attributes provide a clean way to extend Magento 2.
They protect core code and support complex integrations.

They are essential for advanced checkout customization.
Every Magento developer should master them.

However, in case of any query or questions regarding the Magento 2 Extensions, you can create a ticket at webkul.uvdesk.com 

or contact us at store.webkul.com/contacts/ to let us know your views to make the plugin better.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


4 comments

  • Sreedevi
    • Mahesh Singh (Moderator)
  • Raviteja
    • Praful Rajput
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    How to add extension attribute in Magento 2