1

I added a new field on table sales_order_item.

I need to display it on REST API order export, in the "items" node. Is it possible to do that ?

1
  • yes it is possible, in which rest api do you need it? – LucScu Oct 20 '18 at 9:54
3

You can create a module to do it as I did below, I created a module to add the field mpfreegifts_rule_id.

app/code/Vendor/FreeGifts/Plugin/OrderItemRepositoryPlugin.php

<?php

namespace Vendor\FreeGifts\Plugin;

use Magento\Sales\Api\Data\OrderItemExtensionFactory;
use Magento\Sales\Api\Data\OrderItemExtensionInterface;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Api\Data\OrderItemSearchResultInterface;
use Magento\Sales\Api\OrderItemRepositoryInterface;

/**
 * Class OrderItemRepositoryPlugin
 */
class OrderItemRepositoryPlugin
{
    /**
     * Order feedback field name
     */
    const FIELD_NAME = 'mpfreegifts_rule_id';

    /**
     * Order Extension Attributes Factory
     *
     * @var OrderItemExtensionFactory
     */
    protected $extensionFactory;

    /**
     * OrderItemRepositoryPlugin constructor
     *
     * @param OrderItemExtensionFactory $extensionFactory
     */
    public function __construct(OrderItemExtensionFactory $extensionFactory)
    {
        $this->extensionFactory = $extensionFactory;
    }

    /**
     *
     * @param OrderItemRepositoryInterface $subject
     * @param OrderItemInterface $orderItem
     *
     * @return OrderItemInterface
     */
    public function afterGet(OrderItemRepositoryInterface $subject, OrderItemInterface $orderItem)
    {
        $freeGifts = $orderItem->getData(self::FIELD_NAME);
        $extensionAttributes = $orderItem->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
        $extensionAttributes->setMpfreegiftsRuleId($freeGifts);
        $orderItem->setExtensionAttributes($extensionAttributes);

        return $orderItem;
    }

    /**
     *
     * @param OrderItemRepositoryInterface $subject
     * @param OrderItemSearchResultInterface $searchResult
     *
     * @return OrderItemSearchResultInterface
     */
    public function afterGetList(OrderItemRepositoryInterface $subject, OrderItemSearchResultInterface $searchResult)
    {
        $orderItems = $searchResult->getItems();

        foreach ($orderItems as &$item) {
            $freeGifts = $item->getData(self::FIELD_NAME);
            $extensionAttributes = $item->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
            $extensionAttributes->setMpfreegiftsRuleId($freeGifts);
            $item->setExtensionAttributes($extensionAttributes);
        }

        return $searchResult;
    }
}

app/code/Vendor/FreeGifts/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Api\OrderItemRepositoryInterface">
        <plugin name="vendor_freegifts_add_order_extension_attribute"
                type="Vendor\FreeGifts\Plugin\OrderItemRepositoryPlugin" />
    </type>
</config>

app/code/Vendor/FreeGifts/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\OrderItemInterface">
        <attribute code="mpfreegifts_rule_id" type="string" />
    </extension_attributes>
</config>

The result will be like that:

Magento 2 add custom order item field API

Reference: https://www.atwix.com/magento-2/adding-custom-attribute-to-api-response-in-magento-2/

1
  • Thank you. Works like a charm for quote Item attr. – Plim Apr 28 '20 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.