Question

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
    }
}}

?>
Was it helpful?

Solution

Solution 1

Add this in config.xml

   <?xml version="1.0"?>
<config>
  <modules>
    <Froggyline_HappyHour>
      <version>0.0.0</version>
    </Froggyline_HappyHour>
  </modules>
  <adminhtml>  
        <events>
     //second event
               <catalog_product_save_after>
                    <observers>
                        <happyhour>
                            <type>model</type>
                            <class>happyhour/observer</class>
                            <method>product_save_after</method>
                        </happyhour>
                    </observers>
                </catalog_product_save_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 use for 
        $stock->setData('is_in_stock', 1); // Set the Product to InStock                               
        $stock->save(); // Save use for 
    }
}
 }

Solution 2 (Not Tested, But Better Solution)

Add this in config.xml

   <?xml version="1.0"?>
<config>
  <modules>
    <Froggyline_HappyHour>
      <version>0.0.0</version>
    </Froggyline_HappyHour>
  </modules>
  <adminhtml>  
        <events>
     //second event
               <catalog_product_prepare_save>
                <observers>
                    <happyhour>
                        <type>model</type>
                        <class>happyhour/observer</class>
                        <method>product_prepare_save</method>
                    </happyhour>
                </observers>
            </catalog_product_prepare_save>
        </events>
    </adminhtml> 

</config> 

app/code/community/Froggyline/HappyHour/Model/Observer.php

   <?php
class Froggyline_HappyHour_Model_Observer {

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

        if ( $product && $stockData['qty'] ) {

            $stockData->setData('is_in_stock', 1); // Set the Product to InStock                               

        }
    }
 }

OTHER TIPS

I write here only the main steps

1) write a custom module skeleton you can use several "module creators" I personally use this http://www.silksoftware.com/magento-module-creator/

You can set you company and module name and live all other options to NO to create a skeleton module

2) open etc/config and add you Model Name class, end declare the event observers you need

  <global>
    <models>
        <moduloname>
            <class>company_modulo_Model</class>
        </moduloname>
    </models>
    <events>
       < catalog_product_save_after>
        <observers>
            <moduloname>
                <class>moduloname/observer</class>
                <method>yourFunc</method>
            </moduloname>
        </observers>
    </catalog_product_save_after>

3) create Model/Observer.php and write yourFun inside

    <?php  
    class company_Modulo_Model_Observer
{

    public function yourFunc(Varien_Event_Observer $observer) {
    }
 }

For deeper details in the above steps there are tons of other resources, the above is only a sketch of what you have to do

hope it helps

In app/design/adminhtml/default/default/template/catalog/product/tab/inventory.phtml I have changed this:

<?php foreach($this->getStockOption()as $option):?>
<?php $_selected =($option['value']== $this->getFieldValue('is_in_stock'))?'selected="selected"':''?>
<option value="<?php echo $option['value']?>" <?php echo $_selected ?>>
<?php echo $option['label']?></option><?php endforeach;?>

To this:

<?php if(($this->getFieldValue('qty')*1)>0):?>
<option selected="selected"value="1">In Stock</option><?php else:?>
<option selected="selected"value="0">Out of Stock</option><?php endif;?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top