1313namespace Sebwite \SmartSearch \Model \Autocomplete ;
1414
1515use Magento \Catalog \Api \ProductRepositoryInterface ;
16+ use Magento \Catalog \Helper \Image ;
1617use Magento \Framework \Api \FilterBuilder ;
1718use Magento \Framework \Api \Search \FilterGroupBuilder ;
1819use Magento \Framework \Api \Search \SearchCriteriaFactory as FullTextSearchCriteriaFactory ;
2526use Magento \Store \Model \StoreManagerInterface ;
2627
2728/**
28- * Full text search implementation of autocomplete.
29+ * Full text search implementation of autocomplete.
30+ *
31+ * @package Sebwite\SmartSearch
32+ * @author Sebwite
33+ * @copyright Copyright (c) 2015, Sebwite. All rights reserved
2934 */
3035class SearchDataProvider implements DataProviderInterface
3136{
@@ -54,18 +59,24 @@ class SearchDataProvider implements DataProviderInterface
5459
5560 /** @var SearchCriteriaBuilder */
5661 protected $ searchCriteriaBuilder ;
62+
5763 /**
5864 * @var StoreManagerInterface
5965 */
60- private $ storeManager ;
66+ protected $ storeManager ;
67+
6168 /**
6269 * @var PriceCurrencyInterface
6370 */
64- private $ priceCurrency ;
71+ protected $ priceCurrency ;
72+
6573 /**
6674 * @var ProductHelper
6775 */
68- private $ productHelper ;
76+ protected $ productHelper ;
77+
78+ /** @var \Magento\Catalog\Helper\Image */
79+ protected $ imageHelper ;
6980
7081 /**
7182 * Initialize dependencies.
@@ -80,6 +91,7 @@ class SearchDataProvider implements DataProviderInterface
8091 * @param SearchCriteriaBuilder $searchCriteriaBuilder
8192 * @param StoreManagerInterface $storeManager
8293 * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
94+ * @param \Magento\Catalog\Helper\Image $imageHelper
8395 */
8496 public function __construct (
8597 QueryFactory $ queryFactory ,
@@ -91,48 +103,54 @@ public function __construct(
91103 ProductRepositoryInterface $ productRepository ,
92104 SearchCriteriaBuilder $ searchCriteriaBuilder ,
93105 StoreManagerInterface $ storeManager ,
94- PriceCurrencyInterface $ priceCurrency
95- ) {
96- $ this ->queryFactory = $ queryFactory ;
97- $ this ->itemFactory = $ itemFactory ;
98- $ this ->fullTextSearchApi = $ search ;
106+ PriceCurrencyInterface $ priceCurrency ,
107+ Image $ imageHelper
108+ )
109+ {
110+ $ this ->queryFactory = $ queryFactory ;
111+ $ this ->itemFactory = $ itemFactory ;
112+ $ this ->fullTextSearchApi = $ search ;
99113 $ this ->fullTextSearchCriteriaFactory = $ searchCriteriaFactory ;
100- $ this ->filterBuilder = $ filterBuilder ;
101- $ this ->searchFilterGroupBuilder = $ searchFilterGroupBuilder ;
102- $ this ->productRepository = $ productRepository ;
103- $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
104- $ this ->storeManager = $ storeManager ;
105- $ this ->priceCurrency = $ priceCurrency ;
114+ $ this ->filterBuilder = $ filterBuilder ;
115+ $ this ->searchFilterGroupBuilder = $ searchFilterGroupBuilder ;
116+ $ this ->productRepository = $ productRepository ;
117+ $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
118+ $ this ->storeManager = $ storeManager ;
119+ $ this ->priceCurrency = $ priceCurrency ;
120+ $ this ->imageHelper = $ imageHelper ;
106121 }
107122
108123 /**
109- * {@inheritdoc}
124+ * getItems method
125+ *
126+ * @return array
110127 */
111128 public function getItems ()
112129 {
113- $ result = [];
114- $ query = $ this ->queryFactory ->get ()->getQueryText ();
130+ $ result = [ ];
131+ $ query = $ this ->queryFactory ->get ()->getQueryText ();
115132 $ productIds = $ this ->searchProductsFullText ($ query );
116133
117134 // Check if products are found
118- if ($ productIds ) {
135+ if ( $ productIds )
136+ {
119137 $ searchCriteria = $ this ->searchCriteriaBuilder ->addFilter ('entity_id ' , $ productIds , 'in ' )->create ();
120- $ products = $ this ->productRepository ->getList ($ searchCriteria );
121-
122- $ baseUrl = $ this ->storeManager ->getStore ()->getBaseUrl ();
138+ $ products = $ this ->productRepository ->getList ($ searchCriteria );
123139
124- // Loop through products
140+ foreach ( $ products ->getItems () as $ product )
141+ {
125142
126- foreach ( $ products -> getItems () as $ product) {
143+ $ image = $ this -> imageHelper -> init ( $ product, ' product_page_image_small ' )-> getUrl ();
127144
128145 $ resultItem = $ this ->itemFactory ->create ([
129- 'title ' => $ product ->getName (), 'price ' => $ this ->priceCurrency ->format ($ product ->getFinalPrice (), false ),
146+ 'title ' => $ product ->getName (),
147+ 'price ' => $ this ->priceCurrency ->format ($ product ->getFinalPrice (), false ),
130148 'special_price ' => $ this ->priceCurrency ->format ($ product ->getSpecialPrice (), false ),
131149 'has_special_price ' => $ product ->getSpecialPrice () > 0 ? true : false ,
132- 'image ' => str_replace ( ' index.php/ ' , '' , $ baseUrl ) . ' /pub/media/catalog/product ' . $ product -> getImage () ,
150+ 'image ' => $ image ,
133151 'url ' => $ product ->getProductUrl ()
134152 ]);
135- $ result [] = $ resultItem ;
153+ $ result [] = $ resultItem ;
136154 }
137155 }
138156
@@ -142,30 +160,34 @@ public function getItems()
142160 /**
143161 * Perform full text search and find IDs of matching products.
144162 *
145- * @param string
146- * @return int[]
163+ * @param $query
164+ *
165+ * @return array
147166 */
148- private function searchProductsFullText ($ query )
167+ protected function searchProductsFullText ($ query )
149168 {
150169 $ searchCriteria = $ this ->fullTextSearchCriteriaFactory ->create ();
151170
152171 /** To get list of available request names see Magento/CatalogSearch/etc/search_request.xml */
153172 $ searchCriteria ->setRequestName ('quick_search_container ' );
154- $ filter = $ this ->filterBuilder ->setField ('search_term ' )->setValue ($ query )->setConditionType ('like ' )->create ();
173+ $ filter = $ this ->filterBuilder ->setField ('search_term ' )->setValue ($ query )->setConditionType ('like ' )->create ();
155174 $ filterGroup = $ this ->searchFilterGroupBuilder ->addFilter ($ filter )->create ();
156175 $ currentPage = 1 ;
157- $ searchCriteria ->setFilterGroups ([$ filterGroup ])
176+ $ searchCriteria ->setFilterGroups ([ $ filterGroup ])
158177 ->setCurrentPage ($ currentPage )
159178 ->setPageSize (self ::PRODUCTS_NUMBER_IN_SUGGEST );
160179 $ searchResults = $ this ->fullTextSearchApi ->search ($ searchCriteria );
161- $ productIds = [];
180+ $ productIds = [ ];
181+
162182 /**
163183 * Full text search returns document IDs (in this case product IDs),
164184 * so to get products information we need to load them using filtration by these IDs
165185 */
166- foreach ($ searchResults ->getItems () as $ searchDocument ) {
186+ foreach ( $ searchResults ->getItems () as $ searchDocument )
187+ {
167188 $ productIds [] = $ searchDocument ->getId ();
168189 }
190+
169191 return $ productIds ;
170192 }
171193}
0 commit comments