Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Auto-changing the “Stock Availability” from “Out of Stock” to “In Stock” (& vice-versa) on Quantity Change

Tags:

magento

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. The system already automatically changes the Stock Availability to Out of Stock when you set the quantity to 0 and save the product. I would like a way to set it back to In Stock when you set the quantity greater than 0 and save the product.

Well, I think I found a simple way, which in itself makes me nervous. So I wanted to post to you gurus to see if this is safe, correct, and ok to do.

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

All I have to work on at this point is a live site, so you can understand my concern…

Please let me know if this will have the intended effect (it appears so but it seems to simplistic....)

like image 321
Geoff Avatar asked Aug 23 '11 22:08

Geoff


1 Answers

I believe you can use Magento event catalog_product_save_after. Create an observer method that does the following on event catalog_product_save_after.

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
    }
}
like image 200
Bijeesh K G Avatar answered Sep 28 '22 18:09

Bijeesh K G