5

down vote favorite I have installed Amasty order attributes extension

I followed http://magehit.com/blog/how-to-get-value-of-custom-attribute-on-magento-2-rest-api/

and created

app/code/Amasty/Orderattr/etc/extension_attributes.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="Magento\Sales\Api\Data\OrderInterface"> 
   <attribute code="order_source" type="string" />  
 </extension_attributes> 
</config>

2.Created events.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_order_source" instance="Amasty\Orderattr\Observer\Sales\OrderLoadAfter" />
 </event>
</config>

3.created Observer app/code/Amasty/Orderattr/Observer/Sales/OrderLoadAfter.php

    <?php 
     namespace Amasty\Orderattr\Observer\Sales;
     use Magento\Framework\Event\ObserverInterface;
     use Magento\Sales\Api\Data\OrderExtensionInterface ;
     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('order_source');
      $extensionAttributes->setOrderSource($attr);
      $order->setExtensionAttributes($extensionAttributes);
    }
    private function getOrderExtensionDependency()
   {
     $orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(
    '\Magento\Sales\Api\Data\OrderExtension'
    );
     return $orderExtension;
    }
}

After deleting the generation folder ,the OrderExtension.php was auto generated with get and set methods

var\generation\Magento\Sales\Api\Data\OrderExtension.php

But in Api response I did not see any of my custom order attributes for order created by amasty extension.

Where I am going wrong or another way to do this.

  • Did you get any solution for this issue – Ramesh Jun 9 '17 at 12:24

Your Answer

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

Browse other questions tagged or ask your own question.