Display out of stock product only for specific Category in Magento2
To do above things, you need to do this things.
- Vendor\Extension\etc\di.xml
<type name="Magento\CatalogInventory\Helper\Stock">
<plugin name="Vendor_Extension_Stock_Helper" type="Vendor\Extension\Plugin\Helper\Stock" sortOrder="10" disabled="false" />
</type>
- Create the Helper File with arround plugin.
Vendor\Extension\Plugin\Helper\Stock.php
<?php
namespace Vendor\Extension\Plugin\Helper;
class Stock
{
public function aroundAddIsInStockFilterToCollection(\Magento\CatalogInventory\Helper\Stock $subject, \Closure $proceed, $collection)
{
// Do your Logic Here
return $returnValue;
}
}
You should extend the original function ( Magento\CatalogInventory\Model\ProductCollectionStockCondition::apply() ) which is checking the stock configuration.
"Display out of stock products" config in store > Config > catalog > inventory.
You can use preference to extend that apply() function and add additional condition like that.
from:
$this->stockHelper->addIsInStockFilterToCollection($collection);
to:
if(in_array(category_id, ur_list_of_ids))
$this->stockHelper->addIsInStockFilterToCollection($collection);
In this way, you can enable out of stock products in some of the category only.