I would like to know whether it is possible to edit an order via API analog to the functionality in the Magento 2 backend.
Magento 2 API – How to Edit an Order
apimagento2orderssales-order
Related Solutions
First read this below articles:
- https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html
- https://devdocs.magento.com/guides/v2.3/extension-dev-guide/extension_attributes/adding-attributes.html
If you want to add a field them you need to use extension_atttibutes
Extension attribute gives the facility to add a new field to existing API point.
The fields which you have created using https://www.yereone.com/blog/magento-2-how-to-add-new-order-attribute/) to be used as an extension attribute for data Magento\Sales\Api\Data\OrderInterface
.
So, create etc/extension_attributes.xml
at your module folder
<?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="is_important" type="string" /> <!-- type defined as string -->
</extension_attributes>
</config>
Save field to order table
And as you have created the column to an existing order table, So save the create before plugin on \Magento\Sales\Api\OrderRepositoryInterface::save()
.
<?php
namespace Yereone\NewOrderAttribute\Plugin;
use Magento\Framework\Exception\CouldNotSaveException;
class OrderSave
{
...
public function beforeSave(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Magento\Sales\Api\Data\OrderInterface $resultOrder
) {
$extensionAttributes = $resultOrder->getExtensionAttributes();
if (null !== $extensionAttributes) {
$extensionAttributes->getIsImportant()->getValue();
$resultOrder->setIsImportant( $extensionAttributes->getIsImportant()->getValue());
}
return $resultOrder;
}
}
I asked this a while ago -
Adding extension attributes to Order API Endpoint
But never really got a concrete answer of how to pull it off.
I ended up using a third party extension to get around this for my specific use, not too sure if it would work for whatever you're trying.
Best Answer
Out of the box it's not possible as you can see from this file: https://github.com/magento/magento2/blob/develop/app/code/Magento/Sales/etc/webapi.xml
The only modifications you can do to an order via API are: