Hello Magento Friends,

In this Magento Tutorial, I will illustrate How to Update Product Stock Programmatically in Magento 2.

Product stock is a very important thing that the admin needs to manage to avoid out-of-stock situations. Admin can Setup Product Stock in Magento 2 to manage store inventory. Once the product stock is configured, the stock information is updated when the products are sold out. Admin can Get Product Stock Information in Magento 2 easily for smooth management of inventory.

Magento 2 store admin can update the product stock programmatically. Let’s check out the steps for the same.

Steps to Update Product Stock Programmatically in Magento 2:

Step 1: We need to create a “Data.php” file inside our extension at the following path


Now add the code as follows

Step 2: Define the helper class and use it per your requirement. You need to pass product id as a first argument and stock data in an array as a second argument. Following is the example of a stock data array.


That’s it! Using the above method, you can easily Update Product Stock Programmatically in Magento 2. Admin can easily manage out-of-stock products by getting Out of Stock Products List and notifying customers when the product is back in stock through the Out of Stock Notification Module. To not disturb the customer shopping experience, the admin can even shift Out of Stock Products at Last in the product grid.

Happy Coding!

Click to rate this post!
[Total: 6 Average: 4.7]