Magento – Hide out of stock products in Layered Navigation

attributesfilterlayered-navigationout-of-stock

I will describe my magento issue as best I can.

I have shoes size filter in Layered Navigation but for some reason when i press ex. "size 43" then the results display shoes that had number 43 and now they are now out of stock or they are available in other sizes.

What I need is the out of stock products to be displayed normal but when i press some of the filters ex."shoes size or shirt size" then out of stock products or products with that filter that have been sold to not be displayed.

I hope that someone has the answer.

Thanks

Steve

Best Answer

The "Display Out of Stock Products" option set to No will hide products that are completely out of stock. If you want these to be displayed, but also want filters to hide options where the simple product for that option is out of stock, there is a simple solution.

Rather than fiddle about with how the product collection is retrieved and/or displayed, it is simpler to remove these options from the product attribute index. This index is just used for categories so is pretty safe.

In order to not visible simple products that are not configurable children, just add the following join to the _prepareRelationIndex method of Mage/Catalog/Model/Resource/Product/Indexer/Eav/Abstract.php before the insertFromSelect call:

$select->join(
    array('s' => $this->getTable('cataloginventory/stock_item')),
    's.product_id=i.entity_id AND is_in_stock <> 0',
    array()
);

Needless to say, this is a core hack, and so should be actually implemented as a rewrite or observer to prepare_catalog_product_index_select (be careful that this is triggered from other methods too).

As this is making changes to indexing, you'll want to re-run the indexing for catalog_product_attribute (via admin or n98-magerun) and clear the block cache after the code changes are made.

Related Topic