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 ShahCommented Oct 10, 2018 at 13:58
-
@AdityaShah i am using My custom API– Banvari LalCommented Oct 11, 2018 at 5:29
-
inject the custom attribute intoxml, and add an observe for event ``sales_order_load_after– Aditya ShahCommented Oct 11, 2018 at 5:34
-
I have tried it, it didnt work for me– Banvari LalCommented Oct 11, 2018 at 5:59
-
Please post your code here, so we can help.– Aditya ShahCommented Oct 11, 2018 at 6:03
2 Answers
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.
-
I tried using your solution but it is not workig. It is hard to understand what shell i do in the "pay attention here" if i have just to look it or to change it Commented Oct 11, 2021 at 13:10
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/
-
your solution is not working, not populating db column, not adding in api Commented Oct 13, 2021 at 10:44