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;
}
}
Abhishek Tripathi
2,9152 gold badges21 silver badges38 bronze badges
asked Mar 28, 2019 at 4:22
4
  • Hi Meetali, welcome to Magento SE, Please clear generated/ folder or try with di:compile Commented Mar 28, 2019 at 4:42
  • 1
    One more thing you are calling wrong method it should be setDeliveryDate() not setTipAndTrickAttribute() Commented Mar 28, 2019 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. Commented Mar 28, 2019 at 6:46
  • I guess, setting extension attribute value in observer won't work you should try plugin instead of observer. Commented Mar 28, 2019 at 6:59

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.