scopeConfig = $scopeConfig; $this->searchLayer = $layerResolver->get(); $this->recommendationsFactory = $recommendationsFactory; $this->queryResultFactory = $queryResultFactory; } /** * Is Results Count Enabled * * @return bool */ public function isResultsCountEnabled() { return $this->scopeConfig->isSetFlag( self::CONFIG_RESULTS_COUNT_ENABLED, ScopeInterface::SCOPE_STORE ); } /** * @inheritdoc */ public function getItems(QueryInterface $query) { $recommendations = []; if (!$this->isSearchRecommendationsEnabled()) { return []; } foreach ($this->getSearchRecommendations($query) as $recommendation) { $recommendations[] = $this->queryResultFactory->create( [ 'queryText' => $recommendation['query_text'], 'resultsCount' => $recommendation['num_results'], ] ); } return $recommendations; } /** * Return Search Recommendations * * @param QueryInterface $query * @return array */ private function getSearchRecommendations(\Magento\Search\Model\QueryInterface $query) { $recommendations = []; if ($this->isSearchRecommendationsEnabled()) { $productCollection = $this->searchLayer->getProductCollection(); $params = ['store_id' => $productCollection->getStoreId()]; /** @var \Magento\AdvancedSearch\Model\ResourceModel\Recommendations $recommendationsResource */ $recommendationsResource = $this->recommendationsFactory->create(); $recommendations = $recommendationsResource->getRecommendationsByQuery( $query->getQueryText(), $params, $this->getSearchRecommendationsCount() ); } return $recommendations; } /** * Is Search Recommendations Enabled * * @return bool */ private function isSearchRecommendationsEnabled() { return $this->scopeConfig->isSetFlag( self::CONFIG_IS_ENABLED, ScopeInterface::SCOPE_STORE ); } /** * Return Search Recommendations Count * * @return int */ private function getSearchRecommendationsCount() { return (int)$this->scopeConfig->getValue( self::CONFIG_RESULTS_COUNT, ScopeInterface::SCOPE_STORE ); } }