Native Magento comes with a lot of REST APIs for different Entities like Product, Order, Customer, and other APIs.
I want to give a demo for updating bulk products using rest API in Magento 2.
By Default Native Magento doesn’t provide Bulk Product Update API. You need to call REST API to iterate over a loop. At a time you can update only a single product.
For Bulk Update Product Using Magento 2 Rest API, We need to create a custom rest API and you can pass the array of items for a payload to Bulk update Products.
Create a REST API module we need to define first registration.php and module.xml file to register our module.
Let’s my module name is Rbj_Product where Rbj is the Vendor name and Product is the module name.
Our Module resides under app/code/Rbj/Product location.
Path: app/code/Rbj/Product/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Rbj_Product', __DIR__ );
Path: app/code/Rbj/Product/etc/module.xml file,
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Rbj_Product" setup_version="1.0.0" /> </config>
For Create Custom API in Magento 2, we need to create API folder under the module.
Create ProductUpdateManagementInterface.php file for definition of our function to bulk upload product.
Path: app/code/Rbj/Product/Api/ProductUpdateManagementInterface.php
<?php namespace Rbj\Product\Api; /** * @api * @since 101.0.0 */ interface ProductUpdateManagementInterface { /** * Updates the specified products in item array. * * @api * @param mixed $data * @return boolean */ public function updateProduct($products); }
Now we need to define our di.xml file for the set logic of bulk product in the Model file. Using preference we need to map our API to the model file.
Path: app/code/Rbj/Product/etc/di.xml
<?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="Rbj\Product\Api\ProductUpdateManagementInterface" type="Rbj\Product\Model\ProductUpdateManagement"/> </config>
Now we need to create ProductUpdateManagement.php file under the Model folder. File Path, app/code/Rbj/Product/Model/ProductUpdateManagement.php
<?php namespace Rbj\Product\Model; use Rbj\Product\Api\ProductUpdateManagementInterface as ProductApiInterface; class ProductUpdateManagement implements ProductApiInterface { /** * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ public function __construct( \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Psr\Log\LoggerInterface $logger ) { $this->productRepository = $productRepository; $this->logger = $logger; } /** * Updates the specified product from the request payload. * * @api * @param mixed $products * @return boolean */ public function updateProduct($products) { if (!empty($products)) { $error = false; foreach ($products as $product) { try { $sku = $product['sku']; $productObject = $this->productRepository->get($sku); $qty = $product['qty']; $price = $product['price']; $productObject->setPrice($price); $productObject->setStockData( [ 'is_in_stock' => 1, 'qty' => $qty ] ); try { $this->productRepository->save($productObject); } catch (\Exception $e) { throw new StateException(__('Cannot save product.')); } } catch (\Magento\Framework\Exception\LocalizedException $e) { $messages[] = $product['sku'].' =>'.$e->getMessage(); $error = true; } } if ($error) { $this->writeLog(implode(" || ",$messages)); return false; } } return true; } /* log for an API */ public function writeLog($log) { $this->logger->info($log); } }
We need to create updateProduct() in a Model file and define our custom logic for Bulk upload products using a single request call.
In the above demo, we are updating only the price and quantity of items based on item SKU. You need to pass SKU, price, and qty value of the product to bulk product update. You can also add additional logic for updating the product’s other attributes and fields using the above demo.
Now We need to create a REST API URL using the webapi.xml file,
Path: app/code/Rbj/Product/etc/webapi.xml
<?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/products/updateProduct" method="POST"> <service class="Rbj\Product\Api\ProductUpdateManagementInterface" method="updateProduct"/> <resources> <resource ref="anonymous" /> </resources> </route> </routes>
In above webapi.xml file we define our custom route URL for product update API is /rest/V1/products/updateProduct.
So our final URL will be called as {SITE_URL}/rest/V1/products/updateProduct
Used method is updateProduct and we have defined updateProduct() method with update product logic in ProductUpdateManagement.php file.
Your request payload is look like below,
{ "products": [ { "sku":"24-MB01", "qty":"111", "price":"199.95" }, { "sku":"24-MB03", "qty":"1110", "price":"22" } ] }
On the success of REST API, you got true as the response.