Adding product to cart cause error ‘The stock item for Product is not valid.’?

The question:

I’m adding a nominal product programically to the shopping cart. But it throws an exception as 'The stock item for Product is not valid.'. How can I resolve this?

NOTE : $product is getting passed to this function correctly.

My code is as follows.

    /**
     * Add air product to the cart.
     */
    public function addSubscriptionToCart($product)
    {
        try {
            // Create cart instance.
            $cart = Mage::getModel('checkout/cart');

            // Initialize the cart.
            $cart->init();
            $cart->addProduct($product, array('product_id' => $product->getId(), 'qty' => 1)); // This line causes the error.
            $cart->save();

            return true;

        } catch(Exception $e) {
            Mage::log($e->getMessage());
            return false;
        }
    }

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

solved!, Instead of the product object pass product ID to addProduct() method worked. See the corrected code below.

    /**
     * Add air product to the cart.
     */
    public function addSubscriptionToCart($product)
    {
        try {
            // Create cart instance.
            $cart = Mage::getModel('checkout/cart');

            // Initialize the cart.
            $cart->init();
            $cart->addProduct($product->getId()); // pass product ID
            $cart->save();

            return true;

        } catch(Exception $e) {
            Mage::log($e->getMessage());
            return false;
        }
    }

Method 2

I had the same issue. But i was using another code for adding to the cart:

$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->addProduct($_product, 1);
$quote->collectTotals()->save();

And my problem was in wrong product object. I was loading it by attribute “sku”:

Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

When i changed it to simple load

Mage::getModel('catalog/product')->load($id);

then error was resolved.

So problem was in “wrong” product object by loadByAttribute function.

Method 3

Edit
I came across an interesting tidbit while debugging today and thought I would update this answer. When loading a product collection the stock data is added on via observer (appcodecoreMageCatalogInventoryModelObserver.php, addStockStatusToCollection). The method is checking for a “require_stock_items” flag to be set on the collection, if it isn’t set it calls Mage::getModel('cataloginventory/stock_status')->addStockStatusToProducts($productCollection) which is where the “fake” stock item is added to the product collection items. If the flag is set however it calls Mage::getModel('cataloginventory/stock')->addItemsToProducts($productCollection) which adds the actual stock items to the product collection items.

I know this is way old, but when I ran into this problem I noticed that while loading the product using collection methods:

$productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('some_attribute', array('eq' => $someValue)->load();

While loading the product from the above collection, the product will have an imitation stock_item (Varien_Object) instead of a real stock_item (Mage_CatalogInventory_Model_Stock_item). Unfortunately the only way I’ve seen to get the real stock item from here is to load it from a stock item collection and set it on the product:

$stockItem = Mage::getModel('cataloginventory/stock')->getItemCollection()->addFieldToFilter('product_id', array('eq' => $productId))->getFirstItem(); $product->setStockItem($stockItem);

or reload the product the old fashioned way:

$product = Mage::getModel('catalog/product')->load($productId);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment