Skip to content

Magento 2: Getting Issue to Get the extension_attribute Payload PostData in beforePlugin

How to get an shippingAddress extension_attribute postData in before plugin while creating the order via the API?

For example: I have managed to add that field of extension_attributes, in the calling of /rest/V1/carts/mine/shipping-information API with the following payload:

{
  "addressInformation": {
    "shippingAddress": {
      "firstname": "Alok",
      "customer_address_id": 0,
      "save_in_address_book": 0,
      "city": "New York",
      "prefix": "",
      "region_id": 43,
      "postcode": "11501",
      "middlename": "",
      "same_as_billing": 0,
      "telephone": "0123456789",
      "suffix": "",
      "lastname": "Kumar",
      "street": [
        "160 1st St."
      ],
      "vat_id": "",
      "company": "N/A",
      "region": "Bangkok",
      "fax": "",
      "customer_id": 0,
      "country_id": "TH",
      "email": "",
      "extension_attributes" : {
       "address_type_tag": "My Custom Tag String"
    }
    },
    "shipping_method_code": "flatrate",
    "shipping_carrier_code": "flatrate"
  }
}

The extension_attributes load in the shippingAddress node.

I have created db_schema and creates a column of address_type_tag in the quote_address table.

etcextension_attribute.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="MagentoQuoteApiDataAddressInterface">
    <attribute code="address_type_tag" type="string"/>
</extension_attributes>
</config>

etcdi.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoQuoteModelShippingAddressManagement">
        <plugin disabled="false" name="Address_Attr_Plugin_Magento_Quote_Model_ShippingAddressManagement" sortOrder="10" type="AddressAttrPluginMagentoQuoteModelShippingAddressManagement"/>
    </type>
</config>

PluginMagentoQuoteModelShippingAddressManagement.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace AddressAttrPluginMagentoQuoteModel;

use MagentoQuoteApiDataAddressInterface;

class ShippingAddressManagement
{
    protected $logger;

    public function __construct(
        PsrLogLoggerInterface $logger
    ) {
        $this->logger = $logger;
    }

    public function beforeAssign(
        MagentoQuoteModelShippingAddressManagement $subject,
        $cartId,
        MagentoQuoteApiDataAddressInterface $address
    ) {
        /**
         * {@SetLogger}
         */
        $writer = new LaminasLogWriterStream(BP . '/var/log/addressattr.log');
        $logger = new LaminasLogLogger();
        $logger->addWriter($writer);
        //Print PostData 
        $logger->info(json_encode($address->getData()));

        $extAttributes = $address->getExtensionAttributes();
        if (!empty($extAttributes)) {
            try {
                $address->setAddressTypeTag(getAddressTypeTag);
            } catch (Exception $e) {
                $this->logger->critical($e->getMessage());
            }
        }
    }
}

This is my code sample, which I have tried on my end. I have not get the PostData of extension_attribute after hitting the /rest/V1/carts/mine/shipping-information the response of $address->getData() coming null at extension_attribute place even in the $address->getExtensionAttributes() in the plugin.

I want this shippingAddress extension_attributes data in the rest/V1/orders/{order_id} API response.

Do I missed anything in this implementation, please suggest. Thanks in Advance.