开发者

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

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....)


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


Just create cron job with that code snippet:

public function setBackInStock()
{
    $collection = Mage::getResourceModel('cataloginventory/stock_item_collection');
    $outQty = Mage::getStoreConfig('cataloginventory/item/options_min_qty');
    $collection->addFieldToFilter('qty', array('gt' => $outQty));
    $collection->addFieldToFilter('is_in_stock', 0);

    foreach($collection as $item) {
        $item->setData('is_in_stock', 1);
    }
    $collection->save();
}

Small note, you can set cron job for every minute, because if there are no results job won't be consuming time/resources


The Stock Availability text can be handled in the Inventory Tab of the Product settings, same tab as the stock quantity field. Since you have to input your stock qty manually anyway, I would suggest to just change the Stock Availability setting back to 'In Stock' as you enter the new QTY for your product.


This is what I had to do

        var stock_data = new catalogInventoryStockItemUpdateEntity()
        {
            qty = quantity,
            is_in_stock = inStock,
            manage_stock = stockManaged,
            is_in_stockSpecified = true,
        };

is_in_stockSpecified is important

I used SOAP API to update stock status.

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


Another simple solution is to create a stored procedure on DB and call it with with an event

############################################# START : Enable Stock Status
DELIMITER //
CREATE PROCEDURE EnableStock()
BEGIN

-- UPDATE
UPDATE cataloginventory_stock_status
SET stock_status=1
WHERE qty>0;  

-- UPDATE
UPDATE cataloginventory_stock_status
SET stock_status=0
WHERE qty<0;  

END; //
DELIMITER ;

############################################# END : Enable Stock Status

#Create event
CREATE EVENT CallEnableStock
    ON SCHEDULE EVERY 1 HOUR
    DO
      CALL EnableStock();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜