‘Stock item for Product is not valid’ in Magento

This error will occur even if you have disabled handling stock. Essentially Magento is looking for stock data and it isn’t being found. In my case this was perplexing because I disabled handling stock, so my solution involves putting stock data back via my import.

Here is code to fix your stock data for every product:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public function fixStockAction()
{
    // Set store defaults for Magento
    $storeId = Mage::app()->getStore()->getId();
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
 
    $pModel = Mage::getModel('catalog/product');
 
    $products = $pModel->getCollection();
 
    foreach ($products as $product) {
        $stockData = $product->getStockData();
 
        if (!$stockData) {
            $product = $product->load($product->getId());
            $stockData = array(
                'manage_stock' => 1,
                'is_in_stock'  => 1,
                'qty'          => 1
            );
            $product->setStockData($stockData);
 
            try {
                $product->save();
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }
    }
}
public function fixStockAction()
{
    // Set store defaults for Magento
    $storeId = Mage::app()->getStore()->getId();
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    $pModel = Mage::getModel('catalog/product');

    $products = $pModel->getCollection();

    foreach ($products as $product) {
        $stockData = $product->getStockData();

        if (!$stockData) {
            $product = $product->load($product->getId());
            $stockData = array(
                'manage_stock' => 1,
                'is_in_stock'  => 1,
                'qty'          => 1
            );
            $product->setStockData($stockData);

            try {
                $product->save();
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }
    }
}

Run this and everything should be running just peachy again.

Discussion
Rod says:

Hi, Zachary. I’m having this problem, but I’m not an code expert. Could you please explain exacly where and how should I run this script? I tryed to save a .php file in my Magento root, but it returned me an error message when I runned it.

Thanks

Zachary Schuessler says:

Hi Rod!

You would need to create your own module if you were to do it properly.

Would you be able to paste the error somewhere and show it to me?

Hey Zachary,
worked great. Used it on 800 products after fixing a broken database. Only had to run it once, so I took the code out of the function and placed it on the checkout/cart page.
Thanks a bunch.
Cheers
Michele Tecchia

Mike says:

Have a question about using this script with magento 1.7.0.2, if you could email me with the address I provided I would greatly appreciate your time.

Leave a Reply