3

Below script shows out of stock products in only one category with Display Out Of Stock option yes in the admin by using helper file.

Here is the code:

<?php

namespace Vendor\Module\Helper\Catalog;

class Stock extends \Magento\CatalogInventory\Helper\Stock
{    
    public function addIsInStockFilterToCollection($collection)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $category = $objectManager->get('Magento\Framework\Registry')
                                  ->registry('current_category');//get current category

        if(!is_null($category))
        {

            $categoryId = $category->getId(); //current category id
            $stockFlag = 'has_stock_status_filter';

            if (!$collection->hasFlag($stockFlag)) 
            {
                $isShowOutOfStock = $this->scopeConfig->getValue(
                    \Magento\CatalogInventory\Model\Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
                    \Magento\Store\Model\ScopeInterface::SCOPE_STORE
                );
                $resource = $this->getStockStatusResource();

                //100 static category Id 
                if($categoryId == 100)
                {
                   //show display out of stock products for particular category
                   $resource->addStockDataToCollection(
                        $collection,
                        false
                    );
                }
                else
                {
                    $resource->addStockDataToCollection(
                        $collection,
                        $isShowOutOfStock
                    );
                }
                $collection->setFlag($stockFlag, true); 
            }
        }
       }
}

?>

My problem is:

It's working only for category page and the out of stock products coming into the search result page.

I want to show out of stock products for only one category page should hide in all other categories. But the Out of stock products that belong only to the Category Id '100' should show in the search result page.

Please can you help me to fix this issue?

7
  • hi, got any solution?
    – Manjunath
    Feb 6, 2020 at 15:08
  • 1
    Got confused here, you want to show OOS product on listing page or search result page? Feb 17, 2020 at 7:40
  • @AshishRanade, Yes only for specific category, the category id is 100, also in search result page all products which belongs to category id 100
    – Manjunath
    Feb 18, 2020 at 9:24
  • Do you also want to sell those products? Or just display as OOS item?
    – Arif Ahmad
    Feb 20, 2020 at 14:05
  • Do you want to display it on any pages? is it okay to display in view file? Feb 21, 2020 at 9:03

1 Answer 1

0

If you're using magento 2.4 then you have to create a plugin for

Magento\Catalog\Model\Layer

We have to set the Go to Stores > Configuration > Catalog > Inventory > Stock Options->Display Out of Stock Products. This is necessary because Magento doesn't load the is_in_stock filters when we set this option to 'NO'. So we can't put our custom work/conditions when it sets to NO I've debugged it completely and well , So first we have to set it to YES , then I've created a plugin for Magento\Catalog\Model\Layer`and I've handled in such a way that it is now working properly. 1- The Out Of Stock and In Stock products will be shown for specified category 2- Other all the categories of the store will only show InStock products according to our requirement.

The code is below:

public function aroundPrepareProductCollection(CatalogLayer $subject, \Closure $proceed, $collection)

{ // Getting current category Id $categoryId = $subject->getCurrentCategory()->getId(); // Getting specified category whom we wann show OOS products $showAllCategoryId = $this->displayBoth->getShowAllCategoryId();

if($categoryId == $showAllCategoryId) {
    //Get Out Of Stock Products and also In Stock Products of the specified category
    $collection->addFieldToSelect('*')
        ->setFlag('has_stock_status_filter', true)
        ->joinField('stock_item', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id',
            'is_in_stock=0 || is_in_stock=1');
    // Apply to the collection
    $collection->addCategoriesFilter(['eq' => $categoryId]); // Current category filter, it's not require, because PrepareProductCollection() automatically apply filter for current category.
}else{
    //Get only In Stock products to the all the categories instead of above specified one
    $collection->addFieldToSelect('*')
        ->setFlag('has_stock_status_filter', true)
        ->joinField('stock_item', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id',
            'is_in_stock=1');
    //Apply to the collection
    $collection->addCategoriesFilter(['eq' => $categoryId]);
}
return $proceed($collection);

}

This has been tested well and working fine for Magento 2.4 EE and CE

Your Answer

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

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