stockHelper = $stockHelper; $this->scopeConfig = $scopeConfig; } /** * Before prepare product collection handler * * @param \Magento\Catalog\Model\Layer $subject * @param \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection * * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforePrepareProductCollection( \Magento\Catalog\Model\Layer $subject, \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection ) { if ($this->_isEnabledShowOutOfStock()) { return; } $this->stockHelper->addIsInStockFilterToCollection($collection); } /** * Get config value for 'display out of stock' option * * @return bool */ protected function _isEnabledShowOutOfStock() { return $this->scopeConfig->isSetFlag( 'cataloginventory/options/show_out_of_stock', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); } }