Magento Asked on November 27, 2021
I am using this –
extension_attributes.xml
<extension_attributes for="MagentoSalesApiDataOrderInterface">
<attribute code="custom_sales_id" type="string" />
</extension_attributes>
di.xml
<type name="MagentoSalesApiOrderRepositoryInterface">
<plugin name="custom_sales_id" type="CompanyNameModuleNamePluginApiOrderRepository"/>
</type>
OrderRepository.php
<?php
namespace CompanyNameModuleNamePluginApi;
ini_set('display_errors',1);
error_reporting(E_ALL);
use MagentoSalesApiDataOrderExtensionFactory;
use MagentoSalesApiDataOrderExtensionInterface;
use MagentoSalesApiDataOrderInterface;
use MagentoSalesApiDataOrderSearchResultInterface;
use MagentoSalesApiOrderRepositoryInterface;
use MagentoFrameworkExceptionCouldNotSaveException;
/**
* Class OrderRepository
*/
class OrderRepository
{
const CUSTOM_SALES_ID= 'custom_sales_id';
/**
* Order Extension Attributes Factory
*
* @var OrderExtensionFactory
*/
protected $extensionFactory;
/**
* OrderRepositoryPlugin constructor
*
* @param OrderExtensionFactory $extensionFactory
*/
public function __construct(OrderExtensionFactory $extensionFactory)
{
$this->extensionFactory = $extensionFactory;
}
/**
* Add "custom_sales_id" 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 $order)
{
$customSalesId = $order->getData(self::CUSTOM_SALES_ID);
$extensionAttributes = $order->getExtensionAttributes();
$extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
$extensionAttributes->setCustomSalesId($customSalesId );
$order->setExtensionAttributes($extensionAttributes);
return $order;
}
/**
* Add "custom_sales_id" 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 $searchResult)
{
$orders = $searchResult->getItems();
foreach ($orders as &$order) {
$customSalesId = $order->getData(self::CUSTOM_SALES_ID);
$extensionAttributes = $order->getExtensionAttributes();
$extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
$extensionAttributes->setCustomSalesId($customSalesId );
$order->setExtensionAttributes($extensionAttributes);
}
return $searchResult;
}
public function beforeSave(OrderRepositoryInterface $subject, OrderInterface $resultOrder)
{
foreach (['custom_sales_id'] as $field) {
$value = $resultOrder->getData($field);
$resultOrder->setData($field, $value);
}
}
}
custom_file.php
$data_json = [
"entity"=> [
"entity_id" => 76,
"customerId" => 61,
"state" => "complete",
"status" => "complete",
"extension_attributes" =>[
"custom_sales_id" => "test"
]
]
];
echo $data_string = json_encode($data_json);
$ch = curl_init("http://xyz/index.php/rest/V1/orders/create");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data_json));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
$result = curl_exec($ch);
$result = json_decode($result, 1);
echo "-----------update order----------------";
echo "<pre>";
print_r($result);
It showing response but not inserting into database
As I debug afterGet function not calling
public function beforeSave(OrderRepositoryInterface $subject, OrderInterface $order)
{
$extensionAttributes = $order->getExtensionAttributes();
$extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
$resultOrder->setData(self::CUSTOM_SALES_ID, $extensionAttributes->getCustomSalesId());
}
Answered by Kwame on November 27, 2021
Try to replace the beforeSave function code like below in your code.
public function beforeSave(OrderRepositoryInterface $subject, OrderInterface $order) {
$extensionAttributes = $order->getExtensionAttributes() ?: $this->extensionFactory->create();
if ($extensionAttributes !== null && $extensionAttributes->getCustomSalesId() !== null) {
$order->setCustomSalesId($extensionAttributes->getCustomSalesId());
}
return [$order];
}
Hope this works for you. Thanks!
Answered by nishu on November 27, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP