{"id":373,"date":"2019-04-11T05:11:43","date_gmt":"2019-04-11T05:11:43","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/04\/11\/auto-change-stock-availability-on-quantity-update-in-magento-2\/"},"modified":"2025-05-22T16:58:42","modified_gmt":"2025-05-22T11:28:42","slug":"auto-change-stock-availability-on-quantity-update-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/auto-change-stock-availability-on-quantity-update-in-magento-2\/","title":{"rendered":"How to Auto Change &#8220;Stock Availability&#8221; on Quantity Update in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Finding it difficult to manage the inventory status in your Magento 2 store? Do you sometimes forget to update the stock availability status after changing the quantity number in the Magento 2 backend? Ever disappointed a visitor with out of stock status which was not the case actually?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I get you!&nbsp; <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Developers tend to forget the inventory status after updating the quantity. This results in out of stock status in the frontend although you have updated the stock from the backend.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How about implementing a method that <i><strong>auto change &#8220;Stock Availability&#8221; on quantity update in Magento 2<\/strong><\/i>? In that way, you don&#8217;t need to worry about the inventory status. The code below takes care of it!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Auto Change &#8220;Stock Availability&#8221; on Quantity Update in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: <\/strong>Create observer `catalog_product_save_after` in <strong>Vendor &gt; Extension &gt; etc &gt; adminhtml &gt; events.xml<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;event name=\"catalog_product_save_after\">\n    &lt;observer name=\"meetanshi_catalog_product_save_after\" instance=\"Vendor\\Extension\\Observer\\ProductSaveAfter\"\/>\n&lt;\/event><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2: <\/strong>Create <strong>observer file<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extension\\Observer;\nclass ProductSaveAfter implements ObserverInterface\n{\n    protected $stockRegistry;\n    public function __construct(StockRegistryInterface $stockRegistry)\n    {\n        $this->stockRegistry = $stockRegistry;\n    }\n    public function execute(Observer $observer)\n    {\n        try \n        {\n            $product = $observer->getProduct();\n            if ($product->getTypeId() != 'configurable')\n            {\n                $sku = $product->getSku();\n                $stockItem = $this->stockRegistry->getStockItemBySku($sku);\n                $qty = $stockItem->getQty();\n                $stockItem->setIsInStock((bool)$qty);\n                $this->stockRegistry->updateStockItemBySku($sku, $stockItem);\n            }\n        }\n        catch (\\Exception $e) \n        {\n            return $e->getMessage();\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Follow the above steps and you can easily manage the inventory status on quantity change in Magento 2!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> The solution works with Magento 2.2.x<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Finding it difficult to manage the inventory status in your Magento 2 store? Do you sometimes forget to update the stock availability status after changing&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-373","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=373"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/373\/revisions"}],"predecessor-version":[{"id":15473,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/373\/revisions\/15473"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}