Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Hide “Out of Stock” Products in Layered Navigation and Product Filter Magento

If you want to filter your product collection and want to show only in stock product than you can do as following.

You need to change in file Mage/Catalog/Model/Layer.php

Change the following function
public function prepareProductCollection($collection)
{
   $collection
         ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
         ->addMinimalPrice()
         ->addFinalPrice()
         ->addTaxPercents()
         //->addStoreFilter()
         ->addUrlRewrite($this->getCurrentCategory()->getId());
        
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
     Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
     return $this;
 }

To

public function prepareProductCollection($collection)
{
   $collection
         ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
         ->addMinimalPrice()
         ->addFinalPrice()
         ->addTaxPercents()
         //->addStoreFilter()
         ->addUrlRewrite($this->getCurrentCategory()->getId());
        
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
     Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
     //return $this;
$collection->joinField(
       'stock_status',
       'cataloginventory/stock_status',
       'stock_status',
       'product_id=entity_id', array(
       'stock_status' => Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK,
       'website_id' => Mage::app()->getWebsite()->getWebsiteId(),
      )
    );
   return $collection;
 }

Please support us, Like us on Facebook.

  1. Hi,

    Just tried this on Magento 1.8 and it didn't seem to work, out of stock sizes in layered navigation still showed up. Any suggestions?

    ReplyDelete
  2. hello,

    It doesn't work on 1.8 :(

    ReplyDelete
    Replies
    1. It is working for me in magento 1.8 as well.

      Delete
  3. Thank you very much, this worked for me on 1.8

    ReplyDelete
  4. Thanks a lot. Works for me on 1.8 CE.

    ReplyDelete
  5. Hello Patel,
    the problem is that I use configurable products, at the Layered navigation a sea the simple product option, but when i open a configurable its not available!

    Show products out of Stock is ON
    Alow reorder is OFF

    Its possible to hide those products from the layered navigation that aren't in stock.

    Thank you,
    Best regards
    Scamp

    ReplyDelete

 

Copyright @ 2017 HKBlog.