Magento – Stock Item Not Put Back in Stock Automatically Upon Reaching Positive Inventory

inventoryout-of-stock

When a stock item reaches QTY 0 or negative (granted backorders are disabled & QTY for item to become out of stock is 0 as well), is_in_stock is set to 0 automatically.

However, when it then reaches a positive inventory level, is_in_stock is not automatically set to 1. Why is this the case?

Consider the following example:

$product = Mage::getModel('catalog/product')->load($product_id); //where $product_id is an in stock product
$stock_item = $product->getStockItem();
$stock_item->setQty(0);
$stock_item->save();

The product is now listed as out of stock.

$product = Mage::getModel('catalog/product')->load($product_id); //where $product_id is the product now out of stock
$stock_item = $product->getStockItem();
$stock_item->setQty(1);
$stock_item->save();

The product remains out of stock. Should this not be put back into stock?

Best Answer

I ran a grep for "setIsInStock" and there are only a few places where this is set:

What I've found in Mage_CatalogInventory_Model_Stock and Mage_CatalogInventory_Model_Stock_Item is, that there is a check the functions of these classes for the minimum quantity of the product. The backend setting for this (on the product edit page) is "Qty for Item's Status to Become Out of Stock".

This is for example the code in Mage_CatalogInventory_Model_Stock_Item:

/**
     * Before save prepare process
     *
     * @return Mage_CatalogInventory_Model_Stock_Item
     */
    protected function _beforeSave()
    {
        //...

        $isQty = Mage::helper('catalogInventory')->isQty($typeId);

        if ($isQty) {
            if (!$this->verifyStock()) {
                $this->setIsInStock(false)
                    ->setStockStatusChangedAutomaticallyFlag(true);
            }

            //.....

        } else {
            $this->setQty(0);
        }

        return $this;
    }

and verifyStock which also checks for the minimum quantity: $qty <= $this->getMinQty()

/**
     * Chceck if item should be in stock or out of stock based on $qty param of existing item qty
     *
     * @param float|null $qty
     * @return bool true - item in stock | false - item out of stock
     */
    public function verifyStock($qty = null)
    {
        if ($qty === null) {
            $qty = $this->getQty();
        }
        if ($this->getBackorders() == Mage_CatalogInventory_Model_Stock::BACKORDERS_NO && $qty <= $this->getMinQty()) {
            return false;
        }
        return true;
    }

Possible solution:

My guess is, that you have a minimum quantity set which is higher than "1".

Update:

As tested in the comments below, the is_in_stock value is not set back when updating a product in the backend. It seems the only place where a product is set into stock by default is backItemQty() method in Mage_CatalogInventory_Model_Stock when an order is cancelled/returned.

Related Topic