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?

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 :)

| improve this answer | |
-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>
| improve this answer | |
  • 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. – Mike Tim Turner Mar 22 '17 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 Mar 22 '17 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. – Dinesh Yadav Mar 23 '17 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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