Create a Custom REST API for Bulk product update in Magento 2.

Native Magento comes with a lot of REST API for different Entity like Product, Order, Customer and others API.

I want to give a demo for update bulk product using rest api in Magento 2. By Default Native Magento doesn’t provide Bulk Product Update API. You need to call REST API for each item using the foreach loop. At a time you can update an only single product.

For Bulk Update Product Using Magento 2 Rest API, We need to create 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 for register our module.

Lets my module name is Rbj_Product where Rbj is Vendor name and Product is the module name.
Our Module resides under app/code/Rbj/Product location.

Path:  app/code/Rbj/Product/registration.php

Path: app/code/Rbj/Product/etc/module.xml file,

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

Now we need to define our di.xml file for set logic of bulk product in Model file. Using preference we need to map our API to model file.

Path: app/code/Rbj/Product/etc/di.xml

Now we need to create ProductUpdateManagement.php file under the Model folder. File Path, app/code/Rbj/Product/Model/ProductUpdateManagement.php

We need to create updateProduct() in a Model file and define our custom logic for Bulk upload products using 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 update products others attributes and fields using above demo.

Now We need to create REST API URL using webapi.xml file,  Path: app/code/Rbj/Product/etc/webapi.xml

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,

On the success of REST API, you got true as the response.

Bulk update product magento 2
product update magento 2 api

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *