My site is supposed to show out of stock products in some places in the catalogue, so the Catalog > Inventory > Display Out of Stock Products setting is set to "Yes." But I do not want out of stock products to show up in the search. Before upgrading to M2.3, we accomplished this by overriding \Magento\CatalogInventory\Model\Plugin\Layer's beforePrepareProductCollection() function:
public function beforePrepareProductCollection(
\Magento\Catalog\Model\Layer $subject,
\Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection
) {
$_category = $subject->getCurrentCategory();
if (is_object($_category) and in_array($_category->getId(), array(161, 184)))// don't apply to Recently Sold, Our Archive categories
{
return;
}
parent::beforePrepareProductCollection($subject, $collection);
$stockFlag = 'has_stock_status_filter';
if (!$collection->hasFlag($stockFlag)) {
$resource = $this->getStockStatusResource();
$resource->addStockDataToCollection(
$collection,
true // force
);
$collection->setFlag($stockFlag, true);
}
}
protected function getStockStatusResource()
{
if (empty($this->stockStatusResource)) {
$this->stockStatusResource = $this->stockStatusFactory->create();
}
return $this->stockStatusResource;
}
Now that Layer.php is gone in Magento 2.3, I don't know where to start with trying to hide out of stock products in search. I thought maybe I could use Magento\CatalogInventory\Model\Plugin\ProductSearch, but that isn't used at all as far as I can tell.