skip to Main Content

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 MagentoSalesApiDataOrderExtension::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="MagentoSalesModelResourceModelOrderGrid" type="MagentoSalesModelResourceModelGrid">
        <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="MagestoreTipAndTrickObserverSalesOrderLoadAfter" />
</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="MagentoSalesApiDataOrderInterface">
    <attribute code="delivery_date" type="string" />
</extension_attributes>
</config>  

Amos/CustomOrder/Observer/Sales/OrderLoadAfter.php

<?php
namespace AmosCustomOrderObserverSales;
use MagentoFrameworkEventObserverInterface;
class OrderLoadAfter implements ObserverInterface
{
public function execute(MagentoFrameworkEventObserver $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 = MagentoFrameworkAppObjectManager::getInstance()->get(
        'MagentoSalesApiDataOrderExtension'
    );
    return $orderExtension;
}
}

2

Answers


  1. To answer your question about the error you’re using the wrong magic function. Your magic functions for that attribute is setDeliveryDate().

    You also need to make sure your events.xml has the right class for the observer.

    <observer name="sales_order_load_delivery_date" instance="MagestoreTipAndTrickObserverSalesOrderLoadAfter" />
    

    While your observer class is: AmosCustomOrderObserverSalesOrderLoadAfter

    When you are using example material try not to forget to change the class, namespace and functions names among other things when you need to. You may also need an order repository plugin to actually get it into the API response.

    <type name="MagentoSalesApiOrderRepositoryInterface">
        <plugin name="your_name_here_extension_attribute"
                    type="<Vendor><Module>PluginOrderRepositoryPlugin" />
    </type>
    
    Login or Signup to reply.
  2. Yes agree with above answer, you will need to write order repository plugin with get and getlist methods to get it into the API response.

    Below are the code snippet

     public function afterGet(OrderRepositoryInterface $subject, OrderInterface $order)
    {
        $this->addDeliveryDate($order);
        return $order;
    }
    
     public function afterGetList(OrderRepositoryInterface $subject, OrderSearchResultInterface $searchResult)
    {
        $orders = $searchResult->getItems();
        foreach ($orders as $order) {
            $this->addDeliveryDate($order);
        }
        return $searchResult;
    }
    
    private function addDeliveryDate($order)
    {
        $adddeliveryDate = $order->getData(self::DELIVERY_DATE);
       
        $extensionAttributes = $order->getExtensionAttributes() ?: $this->extensionFactory->create();
        $extensionAttributes->setDeliveryDate($adddeliveryDate);
        
        $order->setExtensionAttributes($extensionAttributes);
    
        return $order;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search