3

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

2
  • it is your plugin that is doing the saving, if you show us your plugin, it might become clearer what's not working Commented Jul 20, 2020 at 7:24
  • @HerveTribouilloy i updated my question Commented Jul 20, 2020 at 7:52

2 Answers 2

1

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!

3
  • i am getting $extensionAttributes value is blank Commented Jul 20, 2020 at 12:55
  • Can you please check why you are not getting extension attributes from the order object then a new object must have created from the extension factory class as well. I hope the custom_sales_id column is added to the sales_order table only. please confirm.
    – nishu
    Commented Jul 21, 2020 at 11:22
  • ok will check and let you know Commented Jul 21, 2020 at 11:23
0
public function beforeSave(OrderRepositoryInterface $subject, OrderInterface $order)
{
    $extensionAttributes = $order->getExtensionAttributes();
    $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
    $resultOrder->setData(self::CUSTOM_SALES_ID, $extensionAttributes->getCustomSalesId());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.