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
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
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>
<?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);