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.
-
how you update the stock quantity? manually or by script– saravanaveluJul 24, 2015 at 12:59
-
Until now we were doing it through a php script.But we are about to purchase an extension which will sync products according to our stores inventory.– Tallal HassanJul 24, 2015 at 13:33
-
In script itself, you can change the stock availabillity. Did you tried that?– saravanaveluJul 25, 2015 at 1:59
-
Yeah i guess theres an option in script.But my store owners want it to be predefined without having to change it again and again.– Tallal HassanJul 25, 2015 at 3:51
1 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
}
}
-
Manashvi can you tell where do i have to place this method in magento files? I am just a newbie Jul 24, 2015 at 13:41
-
-
Hello Mashavi i tried following as you wrote but its not working.i cleared my cache after making the module with your files and then tried it on localhost by turning quantity of one of product to zero,then increasing it so it shows on frontend but it does not. Jul 24, 2015 at 19:38
-
Please also note that the observer.php gives error on line 3,could that be the issue? Jul 24, 2015 at 19:41
-
Hi, this script will work when you manually change the stock of the product or if any custom script updates the stock. Also can you let me know what is the error in the observer.php Jul 28, 2015 at 11:25