0

I have added door number custom attribute to sales order address table. But I'm not able to get the vale in Order REST API.

Please help me on this

4
  • anyone knows the workaround – poornima Jun 10 '20 at 9:55
  • Team please suggest me a workaround – poornima Jun 10 '20 at 11:58
  • Friends it is really urgent. Please help me – poornima Jun 10 '20 at 13:04
  • any luck on this @poornima – Devidas Nov 25 '20 at 14:37
0

You have to use extension attribute to expose door number at reset Api.

Create an extension attribute on Magento\Sales\Api\Data\OrderAddressInterface for door no field.

Create extension_attributes.xml at app/code/{Vendorname}/{ModuleName}/etc and add door number is an extension attributes.

<?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\Sales\Api\Data\OrderAddressInterface">
        <attribute code="door_no" type="string" />
    </extension_attributes>
</config>

I assume that door no is varchar field at the database level.

After that, need to expose this field to using a plugin on on \Magento\Sales\Api\OrderRepositoryInterface . Create after plugin on method save .

<?php
namespace Vendorname\ModuleName\Plugin\Magento\Sales\Api;

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderAddressExtensionInterfaceFactory;
use Magento\Sales\Api\Data\OrderAddressExtension;
use Magento\Sales\Api\Data\OrderAddressInterface;

class OrderRepositoryInterface
{
    /**
     * @var OrderAddressExtensionInterfaceFactory
     */
    private $addressExtensionInterfaceFactory;

    public function __construct(
        OrderAddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
    ) {
        $this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
    }

    public function afterGet(
        \Magento\Sales\Api\OrderRepositoryInterface $subject,
        OrderInterface $order
    ) {
        /**
         * @var OrderAddressInterface  $billingAddress
         */
        $billingAddress = $order->getBillingAddress();

        $billingAddressExtensionAttributes = (null !== $billingAddress->getExtensionAttributes())?
            $billingAddress->getExtensionAttributes():
            $this->addressExtensionInterfaceFactory->create();
        $billingAddressExtensionAttributes->setDoorNo($billingAddress->getDoorNo());

        $billingAddress->setExtensionAttributes($billingAddressExtensionAttributes);

        if (!$order->getIsVirtual()) {
            $shippingAddress = $order->getShippingAddress();
            $shippingAddressExtensionAttributes = (null!== $shippingAddress->getExtensionAttributes())?
                $shippingAddress->getExtensionAttributes():
                $this->addressExtensionInterfaceFactory->create();

            $shippingAddressExtensionAttributes->setDoorNo($shippingAddress->getDoorNo());
            $shippingAddress->setExtensionAttributes($shippingAddressExtensionAttributes);
        }

        return $order;
    }
}

Create di.xml at app/code/{Vendorname}/{ModuleName}/etc/

<?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="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin disabled="false" name="add_door_no_to_address" sortOrder="10"
                type="Vendorname\ModuleName\Plugin\Magento\Sales\Api\OrderRepositoryInterface"/>
    </type>
</config>
25
  • Hi But dont we need di.xml file to be updated? Thanks in advance – poornima Jun 10 '20 at 9:33
  • off course. Di XML is needed... – Amit Bera Jun 10 '20 at 9:34
  • Sorry, It is not working. – poornima Jun 10 '20 at 9:38
  • Am I missing anything – poornima Jun 10 '20 at 9:39
  • Are you implementing this code to existing any module? – Amit Bera Jun 10 '20 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.