Question

I followed this guide to add a custom product attribute to quote and sales order item throughout the order process and this works fine.

My custom attribute is added to the sales_order_item table correctly but will not return in the order api call: /rest/V1/orders/X . The item in the order are present but the custom added attribute seems to be missing.

How do I add this sales_order_item attribute to the API output?

I tried it by adding a "extension_attributes.xml" to my custom module (that included the custom attribute: see earlier mentioned link) but that seems to have no effect:

<?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\OrderItemInterface">
        <attribute code="deliverycode" type="string" />
    </extension_attributes>
</config>

Thanks for the help!

Update

Still haven't resolved the issues after trying some minor things. I ran into this topic, which is closely related to my issue. I am trying to adjust the code to suit my needs but I haven't found the answer yet after a few hours of trying to switch the code the be suitable for "sales_order_item". If anyone has any solutions or tips to push me in the right direction they are very much appreciated!

No correct solution

OTHER TIPS

Hi,

Please try to get the order item rest/V1/orders/items/(orderid)

Or else add extension attributes in order api

1. create Vendor\Module\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_attribute" type="string" />
</extension_attributes>
</config>

Then create observer for event ‘sales_order_load_after’

2. Vendor\Module\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_order_attribute" instance="Vendor\Module\Observer\Sales\OrderLoadAfter" />
</event>
</config>

Create a file to get the event data

3. vendor\Module\Observer\Sales\OrderLoadAfter.php

<?php
namespace Vendor\Module\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 = `Get the order item attributes`
    $extensionAttributes->setOrderAttribute($attr);
    $order->setExtensionAttributes($extensionAttributes);
}
private function getOrderExtensionDependency()
{
    $orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(
        '\Magento\Sales\Api\Data\OrderExtension'
    );
    return $orderExtension;
}
}

Please run the upgrade, compile and deploy command

THanks

Let's imagine that you are implementing this for the order service. Then, you request the following service to get a single order:

rest/V1/orders/:orderId

In fact you are requesting the get method of OrderRepository.

So in your extension_attributes.xml you already have this:

<?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\OrderItemInterface">
        <attribute code="deliverycode" type="string" />
    </extension_attributes>
</config>

After doing that you need to implement you Plugin for the OrderRepository, where in your implementation you should follow the example down below:

<?php

namespace YourVendor\Sales\Plugin\Model;

class OrderRepository
{

    protected $orderItemExtension;

    public function __construct(
        \Magento\Sales\Api\Data\OrderItemExtensionInterfaceFactory $orderItemExtension
    )
    {
        $this->orderItemExtension = $orderItemExtension;
    }

    public function afterGet(
        \Magento\Sales\Model\OrderRepository $subject,
        $result
    )
    {
        foreach ($result->getItems() as $item) {
            $deliveryCode = ""; // whatever it comes from...

            $extensionAttributes = $this->orderItemExtension->create();
            $extensionAttributes->setData('deliverycode', $deliveryCode );
            $item->setExtensionAttributes($extensionAttributes);
        }

        return $result;
    }
}

I hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top