Magento – Magento 2 order REST Api append Custom Attribute

custom-attributesextension-attributesmagento2.2rest api

I want to get Custom Attribute value which is already saved in database table, i just want to append it to my Sales order REST APi, i have tried adding extension attribute , i am able to get set and get functions in generation file , how do i show my custom attribute in my REST Api

Best Answer

You will need add an extension_attributes.xml with your custom attr.

Check this example for insert 'custom_attr_name' to order item response, if you want to add a attr for order instead order item just chance order item to just order.

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

Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<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_module_add_order_extension_attribute"
                type="Vendor\Module\Plugin\OrderItemRepositoryPlugin" />
    </type>
</config>

Vendor/Module/Plugin/OrderItemRepositoryPlugin.php

<?php

namespace Vendor\Module\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
{
    /**
        Pay atention here: 

        const CUSTOM_ATTR_NAME = 'custom_attr_name';
    */
    const CUSTOM_ATTR_NAME = 'custom_attr_name';

    /**
     * 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)
    {
        /**
            Pay atention here: 

            $customAttrName = $orderItem->getData(self::CUSTOM_ATTR_NAME);
        */
        $customAttrName = $orderItem->getData(self::CUSTOM_ATTR_NAME);
        $extensionAttributes = $orderItem->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
        /**
            Pay atention here: 

            $extensionAttributes->setCustomAttrName($customAttrName);
        */
        $extensionAttributes->setCustomAttrName($customAttrName);
        $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) {
            /**
                Pay atention here: 

                $customAttrName = $item->getData(self::CUSTOM_ATTR_NAME);
            */
            $customAttrName = $item->getData(self::CUSTOM_ATTR_NAME);
            $extensionAttributes = $item->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
            /**
                Pay atention here: 

                $extensionAttributes->setCustomAttrName($customAttrName);
            */
            $extensionAttributes->setCustomAttrName($customAttrName);
            $item->setExtensionAttributes($extensionAttributes);
        }

        return $searchResult;
    }
}

Look at 'Pay atention here:' comments at OrderItemRepositoryPlugin

Flush your cache and run di:compile.

You also can look at your generated file: OrderItemRepositoryInterface.php And check if you have some function (set or get) to your new attr.

Related Topic