Magento – How to auto turn on ‘Stock Availability’ to ‘In Stock’ after Quantity is updated from 0 to any number greater

productquantity

I am working on a store with thousands of products. I have noticed that when product Quantity is zero (or manually turned to Zero) the Stock Availability goes 'Out of Stock' however when we update the product Quantity to number greater then zero like 10,15 or 200 the 'Stock Availability' stays 'Out of Stock' unless and until manually changed to 'In Stock' so that the product appears on frontend.
I want the 'Stock Availability' to Auto turned to 'In Stock' as we update the Quantity to number greater then Zero. The Quantity in my case gets its values from the store inventory software we don't change it manually.

Best Answer

you can use Magento event catalog_product_save_after.

write this in your config.xml file

<global>
         <events>
        <catalog_product_save_after>
                <observers>
                    <Save_product_data>
                        <type>singleton</type>
                        <class>Magetest_test_Model_Observer</class>
                        <method>changeStockStatus</method>
                    </Save_product_data>
                </observers>
        </catalog_product_save_after>
    </events>
  </global>

Create an observer method changeStockStatus() at below location your_module/Model/Observer.php that does the following on event catalog_product_save_after.

public function changeStockStatus($observer) {
    $product = $observer->getProduct();
    $stockData = $product->getStockData();

    if ( $product && $stockData['qty'] > 0) {
        $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getEntityId()); // Load the stock for this product
        $stock->setData('is_in_stock', 1); // Set the Product to InStock                               
        $stock->save(); // Save
    }
}