I have enabled Show out of stock products in the admin Inventory section.
I have to show all in stock and out of stock products of one category(id=10 and its sub categorysubcategory) in list and search result page. by hiding all out of stock products in remaining categories(But should show all in-stock products).
So when we are in category page(id is 10) need to show all in_stock and out of stock,
category page(id is not 10), Need to show only in_stock products.
If in the search result page, have to show both in_stock and out_of_stock(only from category id 10).
I used the below plugin.
Vendor\Module\etc\di.xml
<type name="Magento\CatalogSearch\Model\Search\IndexBuilder">
<plugin name="Custom_Layernavigation::CustomLayernavigation"
type="Vendor\Module\Plugin\CatalogSearch\Model\Search\IndexBuilder" />
</type>
Then Vendor\Module\Plugin\CatalogSearch\Model\Search\IndexBuilder.php
<?php
use Magento\Framework\Search\RequestInterface;
class IndexBuilder {
protected $_request;
protected $_categoryHelper;
protected $_logger;
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Helper\Category $categoryHelper,
\Magento\Framework\App\Request\Http $request,
\Psr\Log\LoggerInterface $logger
) {
$this->_productCollectionFactory = $productCollectionFactory;
$this->_categoryHelper = $categoryHelper;
$this->_request = $request;
$this->_logger = $logger;
}
public function aroundBuild($subject, callable $proceed, RequestInterface $request) {
$select = $proceed($request);
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->joinField(
'qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'
);
$collection->addAttributeToFilter('qty',['gt'=>0])->load();
$collection->getSelect()
->join(array('cat_index'=> 'catalog_category_product_index_store1'), "cat_index.product_id = e.entity_id")
->where('cat_index.category_id <> 10');
return $select;
}
}
Can someone update me on how to implement this functionality.? I am unable to write a join query to match the above requirement. Please check and update me about this. Thanks for the help.