/** * Add only is in stock products filter to product collection * * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection * @return $this */ public function addIsInStockFilterToCollection($collection) { $websiteId = $this->_storeManager->getStore($collection->getStoreId())->getWebsiteId(); $joinCondition = $this->getConnection()->quoteInto('e.entity_id = stock_status_index.product_id' . ' AND stock_status_index.website_id = ?', $websiteId); $joinCondition .= $this->getConnection()->quoteInto(' AND stock_status_index.stock_id = ?', Stock::DEFAULT_STOCK_ID); $collection->getSelect()->join(['stock_status_index' => $this->getMainTable()], $joinCondition, [])->where('stock_status_index.stock_status=?', Stock\Status::STATUS_IN_STOCK); return $this; }