How to fix the Magento Out of stock bug, version 1.1.x

Posted on 05. Sep, 2008 by Fido in Design, Development, Magento

This is a popular bug that has been plaguing many people with their custom Magento solutions.

Problem: Magento shows products that are in stock as “Out of Stock” – It seems to be popularly reported within the New Product block.

Solution: The attribute “status” has not been selected in the product collection. This status attribute holds the information about whether or not the product is or is not in stock (among other items that determine if the item should be sale-able).

This attribute needs to be selected via the new product block code.

Find: app/code/core/Mage/Catalog/Block/Product/New.php
You will see:

$products   = $product->setStoreId($storeId)->getCollection()
->addAttributeToFilter('news_from_date', array('date'=>true, 'to'=> $todayDate))
->addAttributeToFilter(array(array('attribute'=>'news_to_date', 'date'=>true, 'from'=>$todayDate), array('attribute'=>'news_to_date', 'is' => new Zend_Db_Expr('null'))),'','left')
->addAttributeToSort('news_from_date','desc')
->addAttributeToSelect(array('name', 'price', 'small_image'), 'inner')
->addAttributeToSelect(array('special_price', 'special_from_date', 'special_to_date'), 'left')
;

You need to add: ‘->addAttributeToSelect(’status’);’

$products   = $product->setStoreId($storeId)->getCollection()
->addAttributeToFilter('news_from_date', array('date'=>true, 'to'=> $todayDate))
->addAttributeToFilter(array(array('attribute'=>'news_to_date', 'date'=>true, 'from'=>$todayDate), array('attribute'=>'news_to_date', 'is' => new Zend_Db_Expr('null'))),'','left')
->addAttributeToSort('news_from_date','desc')
->addAttributeToSelect(array('name', 'price', 'small_image'), 'inner')
->addAttributeToSelect(array('special_price', 'special_from_date', 'special_to_date'), 'left')
->addAttributeToSelect('status');
;

Tags: , , , ,

8 Comments

jvalen

05. Sep, 2008

Thank you for publishing this fix! I know many people are having problems with figuring this out

Svemir Brkic

06. Sep, 2008

Same fix can be applied to other boxes, such as recently-viewed box:

app/code/core/Mage/Reports/Block/Product/Viewed.php

$productCollection = Mage::getModel(’catalog/product’)
->getCollection()
->addAttributeToSelect(’name’)
->addAttributeToSelect(’price’)
->addAttributeToSelect(’small_image’)
->addAttributeToSelect(’status’)
->addIdFilter($productIds)
->addUrlRewrite();

Fido

06. Sep, 2008

Thank you both for your comments / contributions!

@Svemir – I have also heard of this problem (but not witnessed it myself) on the Compare item block. This fix can most likely be applied to that as well if anyone runs into that.

AngelEyes

09. Sep, 2008

Thanks chaps, works a treat!

Anywhere else that this could also crop up?

Adam

Fido

09. Sep, 2008

I believe the three mentioned between the article and the comments are the only I’ve run across on the forums

Andreas Reiser

27. Nov, 2009

There is another cause you should investigate before changing the above mentioned files:

If you’ve turned off stock management _after_ creating the affected products, all these products are set to “not in stock”.

In this case, you should (1.) override the “manage stock” option for each affected product, (2.) set the option “in stock” to “yes” and (3.) disable the “manage stock”-override again.

(Or, if you have many products, change this setting in the database directly …)

This hint is for version 1.3.x, however, I think many people are still using the solution by Fido

Andreas

Pravin

23. Dec, 2009

In my magento project whenever i add new product in manage product list and select inventory in stock that time in front view that product’s convert into encode language i can’t read it. if it is in out of stock that time it is normal. please any one having an idea share with us.
Thankx

Eric Jeker

13. Jan, 2010

You also can reset the database with those two SQL request :

UPDATE mage_cataloginventory_stock_item SET is_in_stock = 1, manage_stock = 0, use_config_manage_stock = 1, stock_status_changed_automatically = 0 ;

UPDATE mage_cataloginventory_stock_status SET stock_status = 1 ;

This will deactivate the stock management and set ALL the products as “In Stock”.

Leave a reply