6

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

2
  • Can I have the specific to override/extend this class with a module? Commented Feb 20, 2019 at 14:40
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Jarnail S
    Commented Feb 20, 2019 at 15:27

4 Answers 4

5

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.

3
  • awesome! this saved us hours of research!
    – Haim
    Commented Jan 25, 2017 at 22:24
  • 1
    @Robert Maybe mention the indexer to re-run after this change?
    – snh_nl
    Commented Nov 3, 2018 at 16:30
  • 1
    Anyone had to do this for Magento 2?
    – Loxzibit
    Commented May 15, 2019 at 17:51
0

I had the same problem on Magento 1.9 to hide out of stock products from a filter/layered navigation.

Solution:

Simply make changes in

app\design\frontend\foundation\default\template\catalog\layer\filter.phtml

From:

    <ol>
    <?php foreach ($this->getItems() as $_item): ?>
        <li>
            <?php if ($_item->getCount() > 0): ?>
            <a href="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel() ?></a>
            <?php else: echo $_item->getLabel() ?>
            <?php endif; ?>
            <?php if ($this->shouldDisplayProductCount()): ?>
            (<?php echo $_item->getCount() ?>)
            <?php endif; ?>
        </li>
    <?php endforeach ?>
    </ol>

To:

    <ol>
    <?php foreach ($this->getItems() as $_item): ?>
    <?php if ($_item->getCount() > 0): ?>
        <li>
            <a href="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel() ?></a>
            <?php else: echo $_item->getLabel() ?>
            <?php endif; ?>
            <?php if ($this->shouldDisplayProductCount()): ?>
            (<?php echo $_item->getCount() ?>)
        </li>
    <?php endif; ?>
    <?php endforeach ?>
    </ol>
0

What Robert Egginton wrote works perfectly, but requires a change for Magento 2.

In file vendor/magento/module-catalog/Model/ResourceModel/Product/Indexer/Eav/AbstractEav.php add the following in function _prepareRelationIndexSelect on line 200:

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

Just before

if ($parentIds !== null) { 

And run in console

php bin/magento indexer:reindex catalog_product_attribute

It's still a core hack.

-1

Yes you can hide or show's the out of stock product from magento grid product view

it can manage from admin panel follow the following steps

login admin

go to "system"--> "Configuration"

in that goto left side navigation bar "Catalog"--->"Inventory"

then goto right side tabs there is 2 tabs in that click on "Stock Options"

in that tab change "yes" for "Display Out of Stock Products" Option

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.