0

How to add custom attribute in address for rest api to set and get for view that attribute in magento 2.1.12 ?

1 Answer 1

0

Step 1:

Create a new column and set value for the existing order Firstly, you need to create a new column in table sales_order and name for it set a value for the existing orders. In this post, we named it “tip_and_trick_attribute”.

Step 2: Create a specific new Life

Next, you create a file \app\code\Magento\TipandTrick\etc\extension_attributes.xml in the extension folder with following content:

<?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\OrderInterface">
    <attribute code="tip_and_trick_attribute" type="string" />
</extension_attributes>
</config>

You have to create file extension_attributes.xml to add the custom attribute because Magento 2 doesn’t accept to add a new field in the response on the Rest API

Step 3: Create observe

In this step, you need to create observe for event ‘sales_order_load_after’ through the file \app\code\Magento\TipandTrick\etc\events.xml to add the custom attribute into extension_attributes.xml.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_load_after">
    <observer name="sales_order_load_tip_and_trick_attribute" instance="Magestore\TipAndTrick\Observer\Sales\OrderLoadAfter" />
</event>
</config>

Step 4: Create a file a handle the event

Create a file app\code\Magento\TipandTrick\Observer\Sales\OrderLoadAfter.php to handle the event ‘sales_order_load_after’ which is declared above

<?php
namespace Magestore\TipAndTrick\Observer\Sales;
use Magento\Framework\Event\ObserverInterface;
class OrderLoadAfter implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getOrder();
    $extensionAttributes = $order->getExtensionAttributes();
    if ($extensionAttributes === null) {
        $extensionAttributes = $this->getOrderExtensionDependency();
    }
    $attr = $order->getData('tip_and_trick_attribute');
    $extensionAttributes->setTipAndTrickAttribute($attr);
    $order->setExtensionAttributes($extensionAttributes);
}
private function getOrderExtensionDependency()
{
    $orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(
        '\Magento\Sales\Api\Data\OrderExtension'
    );
    return $orderExtension;
}
}

In execute a function, you get the custom attribute from order data and set it into extension attributes of the orders.

Step 5: Delete folder to apply function

Delete folder var\generation to apply function setTipandTrickAttribute’ and ‘getTipandTrickAttribute’ in file‘var\generation\Magento\Sales\Api\Data\OrderExtension.php’. This file will be auto-created.

After getting an order on Magento 2 Rest API, you can see the custom attribute in the response.

Thanks for reading! for refrence:- https://magehit.com/blog/how-to-get-value-of-custom-attribute-on-magento-2-rest-api/

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.