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 Commented Jun 10, 2020 at 9:55
  • Team please suggest me a workaround Commented Jun 10, 2020 at 11:58
  • Friends it is really urgent. Please help me Commented Jun 10, 2020 at 13:04
  • any luck on this @poornima
    – Devidas
    Commented Nov 25, 2020 at 14:37

2 Answers 2

1

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 Commented Jun 10, 2020 at 9:33
  • off course. Di XML is needed...
    – Amit Bera
    Commented Jun 10, 2020 at 9:34
  • Sorry, It is not working. Commented Jun 10, 2020 at 9:38
  • Am I missing anything Commented Jun 10, 2020 at 9:39
  • Are you implementing this code to existing any module?
    – Amit Bera
    Commented Jun 10, 2020 at 9:40
0
<?php

namespace Vendor\SalesGraphQl\Plugin\Magento\Sales\Api;

use Magento\Sales\Api\Data\OrderExtensionFactory; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\OrderRepositoryInterface;

class OrderRepositoryPlugin { /** * @var OrderExtensionFactory */ private $orderExtensionFactory;

public function __construct(
    OrderExtensionFactory $orderExtensionFactory
) {
    $this->orderExtensionFactory = $orderExtensionFactory;
}

public function afterGet(
    OrderRepositoryInterface $subject,
    OrderInterface $order
) {
    $orderExtensionAttribute = (null !== $order->getExtensionAttributes()) ? $order->getExtensionAttributes() : $this->orderExtensionFactory->create();
    if (!$order->getIsVirtual()) {
        $shippingAddress = $order->getShippingAddress()->getData();
        $orderExtensionAttribute->setShippingAssignments([$shippingAddress]);
    }
    return $order;
}

}

setShippingAssignments -> set value in setShippingAssignments method then only custom value will show in shipping address section of order Rest API.

For billing, you can set custom attribute via extension attribute. please try below code.

 $billingAddress  = $order->getBillingAddress();
        $billingAddressExtensionAttributes  = (null !== $billingAddress->getExtensionAttributes()) ?
            $billingAddress->getExtensionAttributes() :
            $this->orderExtensionFactory->create();
        $billingAddressExtensionAttributes->setCustomerNotes($billingAddress->getCustomerNotes());
        $billingAddress->setExtensionAttributes($billingAddressExtensionAttributes);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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