如何在magento 2.3中获取默认订单Api响应中的自定义订单属性

时间:2019-03-27 13:25:56

标签: magento magento2 laravel-5.7 magento-2.3

我创建了一个名为delivery_date的新自定义订单属性,并在销售订单网格中显示了相同的内容,但是我的订单Api响应中未获得该自定义属性。

我得到的错误是致命错误:未捕获的错误:调用未定义的方法Magento \ Sales \ Api \ Data \ OrderExtension :: setTipAndTrickAttribute()

请帮助。

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;
}
}

1 个答案:

答案 0 :(得分:0)

要回答有关错误的问题,请使用错误的魔术函数。该属性的魔术函数为setDeliveryDate()

您还需要确保events.xml具有适用于观察者的正确类。 <observer name="sales_order_load_delivery_date" instance="Magestore\TipAndTrick\Observer\Sales\OrderLoadAfter" />

您的观察者类别为:Amos\CustomOrder\Observer\Sales\OrderLoadAfter

在使用示例材料时,请不要忘记在需要时更改类,名称空间和函数名称。您可能还需要一个订单存储库插件才能将其实际放入api响应中。

<type name="Magento\Sales\Api\OrderRepositoryInterface"> <plugin name="your_name_here_extension_attribute" type="<Vendor>\<Module>\Plugin\OrderRepositoryPlugin" /> </type>