2

I want to hide out of stock products from showing in search or category pages and I do not want to use the built in feature:

Admin >> System >> Configuration >> Catalog >> Inventory >> Stock Options
Display Out of Stock product -> No

The reason is that I use and sort out of stock products and if I disable them completely I can't filter by Stock status.

How do I put a filter on the category and search collection?

3 Answers 3

1

I have done it and I know it will help a lot of people so this is what I did to achieve this:

Firstly you need to modify the collection which is loaded in:

app\code\core\Mage\Catalog\Block\Product\List.php

You're best making a local copy and editing that:

app\code\local\Mage\Catalog\Block\Product\List.php

Find this line:

$this->_productCollection = $layer->getProductCollection();

Directly under it place this code:

$in_Collection = Mage::getModel('cataloginventory/stock_item')
            ->getCollection()
            ->addFieldToFilter('is_in_stock',1);

$in_Products = array();
foreach($in_Collection as $_collection) {
        $in_Products[] = $_collection->getProductId();
}

$this->_productCollection->addAttributeToSelect('*')
                         ->addAttributeToFilter('entity_id', array('in'=>$in_Products));

I hope this helps :)

0
0

@Mike Tim Turner any idea how to get your solution to work in Magento 2.4.2?

-1

you can put a if condition in your list.phtml file

start the condition before li tag

<ol class="products-list" id="products-list">
    <?php foreach ($_productCollection as $_product): ?>
        <?php if($_product->isSaleable()): ?>  <!-- Added condition here -->
        <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">

and then close this condition after li tag

      </li>
     <?php endif; ?>
    <?php endforeach; ?>
   </ol>
3
  • I already did that but the problem is that the category pages end up only showing about 5 products per page instead of showing on the same page if that makes sense. Commented Mar 22, 2017 at 11:46
  • This is not a good solution, you're simply hiding the products in the template. A better solution would be to exclude them from the product collection.
    – Niels
    Commented Mar 22, 2017 at 15:42
  • 1
    @mizuti Read the question first. He was asking to hide the products from category pages (templates). If you want to give him better suggestion then add comment below his question. Commented Mar 23, 2017 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.