2

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

?>
  • 1
    do you have module related to product to install in magento? – Qaisar Satti Nov 13 '15 at 11:03
  • sorry, i dont have such kind of module. – Baby in Magento Nov 13 '15 at 11:05
  • 1
    are this related to market place? – Qaisar Satti Nov 13 '15 at 11:05
  • actually yes, but first please help me to solve for normal magento backend. as code is inter dependent, it may work for marketplace .... – Baby in Magento Nov 13 '15 at 11:07
  • follow this code and create a module when u finish with that tell i will add tell how you will do that..this is simple module. magento.stackexchange.com/questions/68567/… – Qaisar Satti Nov 13 '15 at 11:11
3

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                               

        }
    }
 }
  • 1
    it is related to cache also i think. – Qaisar Satti Nov 13 '15 at 11:34
  • 1
    remove the <adminhtml> tag and put inside the <global></global> tag – Qaisar Satti Nov 13 '15 at 11:38
  • 1
    now add it inside the <global></global> tag – Qaisar Satti Nov 13 '15 at 11:57
  • 2
    change the event update the code and try and feedback – Qaisar Satti Nov 13 '15 at 11:59
  • 2
    did you change the event? and add as previous code – Qaisar Satti Nov 13 '15 at 12:17
1

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

  • 1
    "modulo" is italian's form of "module" sorry for the typo – Giuseppe Nov 13 '15 at 11:13
  • okay, i i will try this...... – Baby in Magento Nov 13 '15 at 11:14
  • 1
    you're welcome ! – Giuseppe Nov 13 '15 at 16:37
0

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;?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.