REST API for updating existing order item data in Magento 2
Posted By : Prahalad Singh Ranawat | 29-Jul-2022
if you need to update sales_order_item table after placing an order (while your product is based on subscription method), here you will get step by step information on how to update sales order item table data using rest api.
create or add in your existing module custom rest api in magento 2.x.
Step1 : - create interface file in <oodles>/<ProductRE>/Api/GetCustomProductListManagementInterface.php and paste below code
<?php
declare(strict_types=1);
namespace oodles\ProductRE\Api;
interface GetCustomProductListManagementInterface
{
/**
* GET for update order item Data api
* @api
* @return mixed|null
*/
public function updateOrderItem();
}
Step 2: - create file in app/code/oodles/ProductRE/Model/GetCustomProductListManagement.php and paste below code
<?php
declare(strict_types=1);
namespace oodles\ProductRE\Model;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use oodles\ProductRE\Api\GetCustomProductListManagementInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Model\Product;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
class GetCustomProductListManagement implements GetCustomProductListManagementInterface
{
protected $productCollectionFactory;
protected $request;
protected $product;
protected $caloriemanagemt;
protected $_orderItems;
protected $orderItemRepo;
protected $itemFactory;
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
RequestInterface $request,
Product $product,
\Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $orderItems,
\Magento\Sales\Model\Order\ItemFactory $itemFactory,
\Magento\Sales\Api\OrderItemRepositoryInterface $orderItem
){
$this->productCollectionFactory = $productCollectionFactory;
$this->request = $request;
$this->product = $product;
$this->_orderItems = $orderItems;
$this->itemFactory = $itemFactory;
$this->orderItemRepo = $orderItem;
}
/**
* GET for update order item Data api
* @api
* @return mixed|null
*/
public function updateOrderItem(){
//Get base64 order data (name,sku,price,order id,option's data)
$orderDatas = $this->request->getParam('order_data');
//Convert base64 data into array
$ordersData = json_decode(base64_decode($orderDatas),true);
foreach($ordersData as $orderData){
$order = $this->itemFactory->create()->getCollection()->addFieldToFilter('petdata', $orderData[0]["petdata"]);
foreach ($order as $items) {
$itemId = $items->getItemId();
$order = $this->orderItemRepo->get($itemId);
$option = $items->getProductOptions();
$option['info_buyRequest']['options'] = $orderData[0]['options'];//id of option
$option['options']['value'] = $orderData[0]['value']; //name of option
$option['options']['print_value'] = $orderData[0]['print_value']; // name of option
$order->setSku($orderData[0]['sku']);//set new sku
$order->setName($orderData[0]['name']);//set new name of product
$order->setPrice($orderData[0]['price']);// set updated price
$order->setProductOption($option);//set all option values
$order->save();
}
echo "success";
}
}
}
Step 3: - create web api file in app/code/oodles/ProductRE/etc/webapi.xml and paste below code
<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/oodles-productre/updateorderitem" method="POST">
<service class="oodles\ProductRE\Api\GetCustomProductListManagementInterface" method="updateOrderItem"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
Step 4: create di file in app/code/oodles/ProductRE/etc/di.xml and paste below code
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="oodles\ProductRE\Api\GetCustomProductListManagementInterface" type="oodles\ProductRE\Model\GetCustomProductListManagement"/>
</config>
For more details, contact us at [email protected]. Our experts will get back to you.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Prahalad Singh Ranawat
Prahalad Singh Ranawat is a highly skilled backend developer with extensive experience in PHP, Laravel, Magento, Headless Magento, RESTful API, Node.js, and Vue.js. He also possesses knowledge in Shopify. Prahalad has a solid background in working with Postman, Swagger, Git, MySQL, MongoDB, and the LAMP stack. With his passion for learning and creativity, he is constantly exploring new technologies to enhance his skills. He has provided DevOps support and contributed his expertise to a range of projects, including Yumi Paws, OACustomer-Dashboard, Vlad Application, Information Sharing Website, Eating Disorder Intervention, TRO Platform, and SimplyNoted.