0

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

  • you need to make custom api for the same – Aditya Shah Oct 10 '18 at 13:58
  • @AdityaShah i am using My custom API – Banvari Lal Oct 11 '18 at 5:29
  • inject the custom attribute intoxml, and add an observe for event ``sales_order_load_after – Aditya Shah Oct 11 '18 at 5:34
  • I have tried it, it didnt work for me – Banvari Lal Oct 11 '18 at 5:59
  • Please post your code here, so we can help. – Aditya Shah Oct 11 '18 at 6:03
0

Go through below link, this is the correct way to add a custom attribute in Rest API

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

| improve this answer | |
0

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.

| improve this answer | |

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.