2

I have created a new custom order attribute named delivery_date and shown the same in sales order grid but i am not getting the custom attribute in my order Api response.

The error I am getting is Fatal error: Uncaught Error: Call to undefined method

Magento\Sales\Api\Data\OrderExtension::setTipAndTrickAttribute() 

Please help.

app/code/Amos/CustomOrder/etc/di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="delivery_date" xsi:type="string">sales_order.delivery_date</item>
                <item name="no_of_days" xsi:type="string">sales_order.no_of_days</item>
                <item name="no_of_crew" xsi:type="string">sales_order.no_of_crew</item>
            </argument>
        </arguments>
    </virtualType>    
</config>

app/code/Amos/CustomOrder/etc/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_delivery_date" instance="Magestore\TipAndTrick\Observer\Sales\OrderLoadAfter" />
</event>
</config>

Amos/CustomOrder/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="delivery_date" type="string" />
</extension_attributes>
</config>  

Amos/CustomOrder/Observer/Sales/OrderLoadAfter.php

<?php
namespace Amos\CustomOrder\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('delivery_date');
    $extensionAttributes->setTipAndTrickAttribute($attr);
    $order->setExtensionAttributes($extensionAttributes);
}
private function getOrderExtensionDependency()
{
    $orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(
        '\Magento\Sales\Api\Data\OrderExtension'
    );
    return $orderExtension;
}
}
  • Hi Meetali, welcome to Magento SE, Please clear generated/ folder or try with di:compile – Ramkishan Suthar Mar 28 '19 at 4:42
  • 1
    One more thing you are calling wrong method it should be setDeliveryDate() not setTipAndTrickAttribute() – Ramkishan Suthar Mar 28 '19 at 4:49
  • Hi Ramkishan, thank you for the response. Now the error of wrong method is resolved but still i did'nt got the delivery_date attribute in my Api response. – Flying Finner Mar 28 '19 at 6:46
  • I guess, setting extension attribute value in observer won't work you should try plugin instead of observer. – Ramkishan Suthar Mar 28 '19 at 6:59

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.