How to Auto Change “Stock Availability” on Quantity Update in Magento 2
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?
I get you! 😄
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.
How about implementing a method that auto change “Stock Availability” on quantity update in Magento 2? In that way, you don’t need to worry about the inventory status. The code below takes care of it!
Steps to Auto Change “Stock Availability” on Quantity Update in Magento 2:
- Create observer
catalog_product_save_after
in Vendor > Extension > etc > adminhtml > events.xml
1234<?xml version="1.0"?><event name="catalog_product_save_after"><observer name="meetanshi_catalog_product_save_after" instance="Vendor\Extension\Observer\ProductSaveAfter"/></event> - Create observer file
1234567891011121314151617181920212223242526272829<?phpnamespace Vendor\Extension\Observer;class ProductSaveAfter implements ObserverInterface{protected $stockRegistry;public function __construct(StockRegistryInterface $stockRegistry){$this->stockRegistry = $stockRegistry;}public function execute(Observer $observer){try{$product = $observer->getProduct();if ($product->getTypeId() != 'configurable'){$sku = $product->getSku();$stockItem = $this->stockRegistry->getStockItemBySku($sku);$qty = $stockItem->getQty();$stockItem->setIsInStock((bool)$qty);$this->stockRegistry->updateStockItemBySku($sku, $stockItem);}}catch (\Exception $e){return $e->getMessage();}}}
Follow the above steps and you can easily manage the inventory status on quantity change in Magento 2!
Note: The solution works with Magento 2.2.x
Please feel free to post any doubts on the topic in the Comments section below and I’d be happy to help.
Do rate the post with 5 stars!
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
10 Comments
Hi, if i upload qty by csv file the status update with your module?
Hello,
No, because this is the product save event.
Thanks.
I tried this on version magento 2.3.5, but no luck.
Hello,
The above solution works with Magento 2.2.x as mentioned in the post.
Thanks.
why $product->getTypeId() != ‘configurable’ ?
Hello,
It is because the configurable product does not have quantity value.
Thanks.
Worth noting that this doesn’t work with Magento 2.3 by default because of the new MSI modules. We didn’t need MSI however so turned it off and this works great, so many thanks!
Hey, Thank you!
if quantity change after order is shipped/invoice then is this event will be called ?
Hello Jack,
No, the above code will only work when the product is saved.
Thanks.