How to get an order's extension attribute value when creating the order via the API?
For example to create an order I'm calling /rest/V1/carts/mine/payment-information with the following payload:
{
"payment_method": {
"method": "cashondelivery"
},
"email": "[email protected]",
"billingAddress": {
"country_id": "US",
"street": [
"N/A"
],
"telephone": "012345678911",
"postcode": "11501",
"city": "Bangkok",
"firstname": "John",
"lastname": "Key",
"region": "New York",
"region_code": "",
"region_id": 43
},
"extension_attributes" : {
"my_custom_order_attribute": "Custom String"
}
}
Plugin File: AddCustomOrderAttribute.php - Not getting the value of ExtensionAttribute in the $myCustomOrderAttribute even in the $order->getData()
public function setMyCustomOrderAttributeData(OrderInterface $order)
{
/**
* {@SetLogger}
*/
$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/attr.log');
$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);
if ($order instanceof \Magento\Sales\Model\Order) {
$myCustomOrderAttribute = $order->getExtensionAttributes();
} else {
$orderModel = $this->orderFactory->create();
$orderModel->load($order->getId());
$myCustomOrderAttribute = $orderModel->getMyCustomOrderAttribute();
}
$logger->info(json_encode($order->getData()));
$extensionAttributes = $order->getExtensionAttributes();
$orderExtensionAttributes = $extensionAttributes ? $extensionAttributes
: $this->orderExtensionFactory->create();
$orderExtensionAttributes->setMyCustomOrderAttribute($myCustomOrderAttribute);
$order->setExtensionAttributes($orderExtensionAttributes);
}
/**
* Add "my_custom_order_attribute" extension attribute to order data object
* to make it accessible in API data
*
* @param OrderRepositoryInterface $subject
* @param OrderSearchResultInterface $searchResult
*
* @return OrderSearchResultInterface
*/
public function afterGetList(
OrderRepositoryInterface $subject,
OrderSearchResultInterface $orderSearchResult
) {
foreach ($orderSearchResult->getItems() as $order) {
$this->setMyCustomOrderAttributeData($order);
}
return $orderSearchResult;
}
/**
* Add "my_custom_order_attribute" extension attribute to order data object
* to make it accessible in API data
*
* @param OrderRepositoryInterface $subject
* @param OrderInterface $order
*
* @return OrderInterface
*/
public function afterGet(
OrderRepositoryInterface $subject,
OrderInterface $resultOrder
) {
$this->setMyCustomOrderAttributeData($resultOrder);
return $resultOrder;
}
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="my_custom_order_attribute" type="string"/>
</extension_attributes>
</config>
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\OrderRepositoryInterface">
<plugin name="ord_attr_get_order_extension_attributes" type="Ord\Attr\Plugin\OrderRepositoryPlugin" />
</type>
</config>
setAttribute is working fine but getting attribute value from API in this code is not working. Please help me to fix this issue.
Thanks in Advance!!