I wish to update the stock status while saving the product.

If the quantity is 0, then I wish to update the stock status as out of stock. If the quantity is 1 and above, I wish to update the stock status as In stock.

Please provide me a solution to achieve it.

I have tried below

I have observed the event 'catalog_product_save_after'


 public function execute(\Magento\Framework\Event\Observer $observer)
        try {
            $product = $observer->getEvent()->getProduct();

                $sku = $product->getSku();
                $stockItem = $this->stockRegistry->getStockItemBySku($sku);
                $qty = $stockItem->getQty();
                $this->stockRegistry->updateStockItemBySku($sku, $stockItem);

        } catch (\Exception $exception) {



But no luck.

The stock status is not automatically or manually updated while the quantity changes

  • It's a default functionality of Magento 2.2.4 and also working in default Magento. For check Disable your third party extension or check your customization and check. – Kirti Nariya May 14 '19 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.