How To

How to Update Product Stock Programmatically in Magento 2

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

app\code\Vendor\Extension\Helper\

Now add the code as follows

<?php
namespace Vendor\Extension\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $_productobj;
    protected $_stockRegistry;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Catalog\Model\Product $productobj,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, 
    ) {
        $this->_productobj = $productobj;
        $this->_stockRegistry = $stockRegistry;
        parent::__construct($context);
    }

    public function updateProductStock($productId,$stockData) {
        $product=$this->_productobj->load($productId); 
        $stockItem=$this->_stockRegistry->getStockItem($productId); 
        $stockItem->setData('is_in_stock',$stockData['is_in_stock']); 
        $stockItem->setData('qty',$stockData['qty']); 
        $stockItem->setData('manage_stock',$stockData['manage_stock']);
        $stockItem->setData('use_config_notify_stock_qty',1);
        $stockItem->save(); 
        $product->save(); 
    }
}

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.

$stockData = array('is_in_stock'=>1,'qty'=> 5, 'manage_stock'=> 1);

Conclusion:

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]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

19 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

7 days ago