assume the product quantity is "0", once we edit the quantity to "1". again we have to change from
"out of stock" to " in stock" to buy in site
So I’ve been looking for a way to change the Stock Availability back to In Stock when the quantity field is greater than 0.
here someone posted solution : https://stackoverflow.com/questions/7168295/magento-auto-changing-the-stock-availability-from-out-of-stock-to-in-stock
we can use Magento event catalog_product_save_after. Create an observer method that does the following on event catalog_product_save_after.
can anyone please explain me in detail what and all needs to be done for this. means in which file which code we have to add ?
public function catalog_product_save_after($observer) {
$product = $observer->getProduct();
$stockData = $product->getStockData();
if ( $product && $stockData['qty'] ) {
$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
}
}
as Qaisar Satti said i am trying this :
app/etc/modules/Froggyline_HappyHour.xml
<?xml version="1.0"?>
<config>
<modules>
<Froggyline_HappyHour>
<active>true</active>
<codePool>community</codePool>
<version>0.0.0</version>
</Froggyline_HappyHour>
</modules>
</config>
app/code/community/Froggyline/HappyHour/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Froggyline_HappyHour>
<version>0.0.0</version>
</Froggyline_HappyHour>
</modules>
<adminhtml>
<events>
<cataloginventory_stock_item_save_commit_after>
<observers>
<happyhour>
<type>model</type>
<class>happyhour/observer</class>
<method>product_save_after</method>
</happyhour>
</observers>
</cataloginventory_stock_item_save_commit_after>
</events>
</adminhtml>
</config>
app/code/community/Froggyline/HappyHour/Model/Observer.php
<?php
class Froggyline_HappyHour_Model_Observer {
public function product_save_after($observer) {
$product = $observer->getProduct();
$stockData = $product->getStockData();
if ( $product && $stockData['qty'] ) {
$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
}
}}
?>