I am using this -
extension_attributes.xml
<extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
<attribute code="custom_sales_id" type="string" />
</extension_attributes>
di.xml
<type name="Magento\Sales\Api\OrderRepositoryInterface">
<plugin name="custom_sales_id" type="CompanyName\ModuleName\Plugin\Api\OrderRepository"/>
</type>
OrderRepository.php
<?php
namespace CompanyName\ModuleName\Plugin\Api;
ini_set('display_errors',1);
error_reporting(E_ALL);
use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderExtensionInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Exception\CouldNotSaveException;
/**
* 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