How to display out of stock products only in one category with "Display Out Of Stock" option "No" in the admin?


The out of stock filter is added to categories in a plugin method in Magento\CatalogInventory\Model\Plugin\Layer. If you want to hook into this logic you can create an around plugin method to the beforePrepareProductCollection method. That can be done for example like this:

Add plugin definition in your di.xml:

<type name="Magento\CatalogInventory\Model\Plugin\Layer">
    <plugin name="Yormodule_CatalogInventory_Layer" type="Vendor\Module\Plugin\CatalogInventoryLayer" sortOrder="10" disabled="false"  />

Create a file in your module folder in Plugin/CatalogInventoryLayer.php with the following content

namespace Vendor\Module\Plugin;

class CatalogInventoryLayer{

    protected $_logger;

    public function __construct( 
        \Psr\Log\LoggerInterface $logger
            $this->_logger = $logger;

    public function aroundBeforePrepareProductCollection(
        \Magento\CatalogInventory\Model\Plugin\Layer $subject,
        callable $proceed,
        \Magento\Catalog\Model\Layer $layer,
        \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection)
        //skip out of stock filter in Magento\CatalogInventory\Model\Plugin::beforePrepareProductCollection 
        //for your category here - you may use some configuration for that
        $this->_logger->debug("my category id ... " . $layer->getCurrentCategory()->getId());
        if ($layer->getCurrentCategory()->getId() != [YOUR CATEGORY ID WITH OUT OF STOCK PRODUCTS]){
            return $proceed($layer,$collection);


Anyway it may be that there are some other calls affecting the building of the collection - in that case you should debug in Magento\CatalogInventory\Helper\Stock::addIsInStockFilterToCollection method

  • hi @HelgeB, its not working – jafar pinjar Feb 6 '20 at 9:59

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.