if the number of products falls below 1, then it will become out of stock, but if you increase the products manually, then the status will remain out of stock. How can I automatically change the status to In Stock if the goods have been added manually?

I tried using the module from this answer, but it doesn't work.

My Magento v 2.3.2

1 Answer 1


I assume you know the necessary files for creating a module, one all basic files generated, create below files:

  1. Vendor/Module/etc/events.xml

     <?xml version="1.0" ?>
     <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
         <event name="catalog_product_save_after">
             <observer instance="Vendor\Module\Observer\Backend\Catalog\ProductSaveAfter" name="vendor_module_observer_backend_catalog_productsaveafter_catalog_product_save_after"/>
  2. Vendor/Module/Observer/Backend/Catalog/ProductSaveAfter.php

     namespace Vendor\Module\Observer\Backend\Catalog;
     class ProductSaveAfter implements \Magento\Framework\Event\ObserverInterface
         protected $product;
         protected $stockStateInterface;
         protected $stockRegistry;
         public function __construct(
             \Magento\Catalog\Model\Product $product,
             \Magento\CatalogInventory\Api\StockStateInterface $stockStateInterface,
             \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
         ) {
             $this->product = $product;
             $this->stockStateInterface = $stockStateInterface;
             $this->stockRegistry = $stockRegistry;
         public function execute(
             \Magento\Framework\Event\Observer $observer
         ) {
             try {
                 $productObserver = $observer->getProduct();
                 if($productObserver->getTypeId() != 'configurable') {
                     $productId = $productObserver->getId();
                     $stockItem = $this->stockRegistry->getStockItem($productId);
                     $productQty = $stockItem->getQty();
                     if($productQty > 0) {
                     } else {
             } catch (\Exception $e) {
                 return $e->getMessage();

Run setup:upgrade and necessary commands depending on your mode and now change stock of the product and see, if stock is 0, it will automatically change stock status to 'Out of stock' and if stock is 1 or above, it will automatically change stock to 'In stock'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.