3

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?

2 Answers 2

2

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.

3
  • thanks for the input. I have the default settings for "QTY for Item's Status to Become Out of Stock" which is 0. I've tested this on a default Magento instance and it doesn't behave accordingly either.
    – Moose
    Jul 1, 2015 at 12:48
  • Just tried it myself with a new CE 1.9.0.1 (which I had just available) and it's not set back to stock either. According to the source code I think the only place where it's set back to stock automatically is when an order is cancelled and the item is set back to stock. I'll update my answer. Jul 1, 2015 at 14:49
  • 1
    Going to have to go about my own implementation thereof. Definitely not efficient having to manually do this on a per product basis. Thanks for the insights, @Anna
    – Moose
    Jul 1, 2015 at 18:57
1

for others reading this: there's free module on magento connect does exactly what you're looking for. Switching the inventory level automatically to in stock when it's back in the positive numbers > 0

I think it's a lack in Magento and this should have been implemented in all Magento shops!

https://www.magentocommerce.com/magento-connect/back-in-stock-status-update.html

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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