Magento 2: Getting extension attributes in ShippingInformation

Magento 2: Getting extension attributes in ShippingInformation

July 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2: Getting extension attributes in ShippingInformation

Magento 2: Retrieving Custom Extension Attributes in Shipping Information Sidebar

In Magento 2, you can add custom extension attributes to the quote object during checkout. This allows you to store and retrieve data that isn’t part of the core Magento functionality. In this guide, we’ll explore how to save these custom extension attributes and access them within the shipping information sidebar.

Saving Custom Extension Attributes

To save your custom extension attributes, you need to create a plugin for the ShippingInformationManagement class. This allows you to intercept the save process and add your data to the quote object.

First, define your extension attributes in an XML file:

<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="customfield1" type="string" />
       <attribute code="customfield2" type="string" />
   </extension_attributes>
</config>

Next, create a plugin to save these attributes:

<?php

namespace VendorName\ModuleName\Plugin\Quote;

use Magento\Quote\Model\QuoteRepository;

class SaveToQuote
{
    protected $quoteRepository;
    protected $logger;

    public function __construct(
        QuoteRepository $quoteRepository,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->logger = $logger;
    }

    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    ) {
        if (!$extAttributes = $addressInformation->getExtensionAttributes())
            return;

        $quote = $this->quoteRepository->getActive($cartId);

        $quote->setCustomfield1($extAttributes->getCustomfield1());
        $quote->setCustomfield2($extAttributes->getCustomfield2());
    }
}

This plugin intercepts the beforeSaveAddressInformation method and saves your custom fields to the quote object.

Accessing Custom Extension Attributes

To access these attributes in the shipping information sidebar, you need to modify the Magento_Checkout/js/view/shipping-information.js file.

First, ensure that your custom extension attributes are being passed correctly. You should have a file like view/frontend/web/js/model/shipping-save-processor/payload-extender-override.js:

define([
    'jquery',
], function ($) {
    'use strict';
    return function (payloadExtender) {

        payloadExtender.addressInformation['extension_attributes'] = {
            customfield1: $('[name="customfields[customfield1]"]').val(),
            customfield2: $('[name="customfields[customfield2]"]').val()
        };
        console.log('payloadExtender:', payloadExtender.addressInformation); // working, shows extensionattributes

        return payloadExtender;
    };
});

This file ensures that your custom fields are being sent along with the shipping address information.

Now, let’s modify Magento_Checkout/js/view/shipping-information.js:

define([
    'jquery',
    'uiComponent',
    'Magento_Checkout/js/model/quote',
    'Magento_Checkout/js/model/step-navigator',
    'Magento_Checkout/js/model/sidebar'
], function ($, Component, quote, stepNavigator, sidebarModel) {
    'use strict';

    var mixin = {

        /**
         * @return {String}
         */
        getShippingMethodTitle: function () {
            var shippingMethod = quote.shippingMethod();

            if (shippingMethod) {
                var shippingMethodContent = shippingMethod['carrier_title'] + ' - ' + shippingMethod['method_title'];

                var shippingMethodCode = shippingMethod['carrier_code'] + '_' + shippingMethod['method_code'];
                if (shippingMethodCode === 'customshipping_customshipping') {

                    console.log("SHIPPING METHOD", shippingMethod, quote);
                    // EXTENSION ATTRIBUTES NOT AVAILABLE HERE, I want to change shipping method content
                    var extAttributes = quote.shippingAddress.extension_attributes;
                    if (extAttributes) {
                        var customfield1 = extAttributes.customfield1;
                        var customfield2 = extAttributes.customfield2;

                        // You can use these values here. For example:
                        shippingMethodContent += " - Custom Field 1: " + customfield1;
                        shippingMethodContent += " - Custom Field 2: " + customfield2;
                    }
                }

                return shippingMethodContent;
            }
            return '';
        }
    };

    return function (target) {
        return target.extend(mixin);
    };
});

Here, we’re accessing the extension_attributes object from the shippingAddress property of the quote model. This allows us to retrieve your custom fields.

Conclusion

In this guide, we’ve explored how to save and access custom extension attributes in Magento 2 during the checkout process. By using a plugin for the ShippingInformationManagement class, you can add your data to the quote object. Then, by modifying the Magento_Checkout/js/view/shipping-information.js file, you can retrieve these custom fields within the shipping information sidebar.

Remember to replace ‘VendorName’ and ‘ModuleName’ with your actual vendor name and module name in all the example files.

For more detailed tutorials on Magento 2 development, consider visiting Laravel Company. We offer comprehensive guides on Magento 2 setup, extension development, and best practices for improving performance and security.

Magento 2 Checkout API Extensions

Our network