Update product stock programmatically in magento2
Here, we learn how to update product stock programmatically in Magento 2
To update product stock programmatically in Magento 2 is a common requirement when working with automated inventory systems, third-party integrations, or bulk product updates.
This is essential for building advanced inventory workflows and integrations. You can automate stock updates, improve inventory accuracy, and ensure a smooth shopping experience for your customers.
Magento 2 allows developers to update product stock using its inventory APIs and service contracts.
This ensures that Magento’s default inventory logic—such as out-of-stock handling, backorders, and stock notifications—continues to function correctly.
Here, we define a function in the helper file of the module to update product stock and use this function where we want to update product stock.
<?php
namespace YourNameSpace\ModuleName\Helper;
use Magento\Framework\App\Filesystem\DirectoryList;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $_product;
/**
* @var Magento\CatalogInventory\Api\StockStateInterface
*/
protected $_stockStateInterface;
/**
* @var Magento\CatalogInventory\Api\StockRegistryInterface
*/
protected $_stockRegistry;
/**
* @param Magento\Framework\App\Helper\Context $context
* @param Magento\Catalog\Model\Product $product
* @param Magento\CatalogInventory\Api\StockStateInterface $stockStateInterface,
* @param Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Catalog\Model\Product $product,
\Magento\CatalogInventory\Api\StockStateInterface $stockStateInterface,
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
) {
$this->_product = $product;
$this->_stockStateInterface = $stockStateInterface;
$this->_stockRegistry = $stockRegistry;
parent::__construct($context);
}
/**
* For Update stock of product
* @param int $productId which stock you want to update
* @param array $stockData your updated data
* @return void
*/
public function updateProductStock($productId,$stockData) {
$product=$this->_product->load($productId); //load product which you want to update stock
$stockItem=$this->_stockRegistry->getStockItem($productId); // load stock of that product
$stockItem->setData('is_in_stock',$stockData['is_in_stock']); //set updated data as your requirement
$stockItem->setData('qty',$stockData['qty']); //set updated quantity
$stockItem->setData('manage_stock',$stockData['manage_stock']);
$stockItem->setData('use_config_notify_stock_qty',1);
$stockItem->save(); //save stock of item
}
}
By using Magento’s built-in inventory APIs and service contracts, you can update stock safely without modifying core files.
This ensures better performance, upgrade compatibility, and seamless integration with Magento’s default inventory features such as stock status, backorders, and notifications.
I hope you liked it. Also, do check out Magento 2 Google Shopping Feed extension, which allows you to showcase your Magento 2 online store’s products on Google using Shopping Feeds.
For technical assistance, please get in touch with us via email at support@webkul.com.
Discover powerful solutions to enhance your Magento 2 store by exploring our Magento 2 plugins page.
Bring your vision to life with custom-built solutions—hire skilled Magento 2 developers today.
Happy Coding!!