@@ -76,12 +76,12 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
76
76
protected $ linkInitializer ;
77
77
78
78
/*
79
- * @param \Magento\Catalog\Model\Product\LinkTypeProvider
79
+ * @var \Magento\Catalog\Model\Product\LinkTypeProvider
80
80
*/
81
81
protected $ linkTypeProvider ;
82
82
83
83
/*
84
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
84
+ * @var \Magento\Store\Model\StoreManagerInterface
85
85
*/
86
86
protected $ storeManager ;
87
87
@@ -140,6 +140,25 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
140
140
*/
141
141
protected $ extensionAttributesJoinProcessor ;
142
142
143
+ /**
144
+ * @var \Magento\Framework\Search\Request\Builder
145
+ */
146
+ private $ requestBuilder ;
147
+
148
+ /**
149
+ * @var \Magento\Framework\Search\SearchEngineInterface
150
+ */
151
+ private $ searchEngine ;
152
+
153
+ /**
154
+ * @var SearchResponseBuilder
155
+ */
156
+ private $ searchResponseBuilder ;
157
+ /**
158
+ * @var \Magento\Framework\App\Config\ScopeConfigInterface
159
+ */
160
+ private $ scopeConfig ;
161
+
143
162
/**
144
163
* @param ProductFactory $productFactory
145
164
* @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $initializationHelper
@@ -162,6 +181,10 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
162
181
* @param \Magento\Eav\Model\Config $eavConfig
163
182
* @param ImageProcessorInterface $imageProcessor
164
183
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
184
+ * @param \Magento\Framework\Search\Request\Builder $requestBuilder
185
+ * @param \Magento\Framework\Search\SearchEngineInterface $searchEngine
186
+ * @param SearchResponseBuilder $searchResponseBuilder
187
+ * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
165
188
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
166
189
*/
167
190
public function __construct (
@@ -185,7 +208,11 @@ public function __construct(
185
208
MimeTypeExtensionMap $ mimeTypeExtensionMap ,
186
209
\Magento \Eav \Model \Config $ eavConfig ,
187
210
ImageProcessorInterface $ imageProcessor ,
188
- \Magento \Framework \Api \ExtensionAttribute \JoinProcessorInterface $ extensionAttributesJoinProcessor
211
+ \Magento \Framework \Api \ExtensionAttribute \JoinProcessorInterface $ extensionAttributesJoinProcessor ,
212
+ \Magento \Framework \Search \Request \Builder $ requestBuilder ,
213
+ \Magento \Framework \Search \SearchEngineInterface $ searchEngine ,
214
+ SearchResponseBuilder $ searchResponseBuilder ,
215
+ \Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig
189
216
) {
190
217
$ this ->productFactory = $ productFactory ;
191
218
$ this ->collectionFactory = $ collectionFactory ;
@@ -208,6 +235,10 @@ public function __construct(
208
235
$ this ->eavConfig = $ eavConfig ;
209
236
$ this ->imageProcessor = $ imageProcessor ;
210
237
$ this ->extensionAttributesJoinProcessor = $ extensionAttributesJoinProcessor ;
238
+ $ this ->requestBuilder = $ requestBuilder ;
239
+ $ this ->searchEngine = $ searchEngine ;
240
+ $ this ->searchResponseBuilder = $ searchResponseBuilder ;
241
+ $ this ->scopeConfig = $ scopeConfig ;
211
242
}
212
243
213
244
/**
@@ -690,6 +721,45 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
690
721
return $ searchResult ;
691
722
}
692
723
724
+ /**
725
+ * @param \Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria
726
+ * @return \Magento\Framework\Api\Search\SearchResultInterface
727
+ */
728
+ public function search (\Magento \Framework \Api \Search \SearchCriteriaInterface $ searchCriteria )
729
+ {
730
+ $ this ->requestBuilder ->setRequestName ($ searchCriteria ->getRequestName ());
731
+
732
+ $ searchTerm = $ searchCriteria ->getSearchTerm ();
733
+ if (!empty ($ searchTerm )) {
734
+ $ this ->requestBuilder ->bind ('search_term ' , $ searchTerm );
735
+ }
736
+
737
+ $ storeId = $ this ->storeManager ->getStore (true )->getId ();
738
+ $ this ->requestBuilder ->bindDimension ('scope ' , $ storeId );
739
+
740
+ foreach ($ searchCriteria ->getFilterGroups () as $ filterGroup ) {
741
+ foreach ($ filterGroup ->getFilters () as $ filter ) {
742
+ $ this ->addFieldToFilter ($ filter ->getField (), $ filter ->getValue ());
743
+ }
744
+ }
745
+
746
+ $ priceRangeCalculation = $ this ->scopeConfig ->getValue (
747
+ \Magento \Catalog \Model \Layer \Filter \Dynamic \AlgorithmFactory::XML_PATH_RANGE_CALCULATION ,
748
+ \Magento \Store \Model \ScopeInterface::SCOPE_STORE
749
+ );
750
+ if ($ priceRangeCalculation ) {
751
+ $ this ->requestBuilder ->bind ('price_dynamic_algorithm ' , $ priceRangeCalculation );
752
+ }
753
+
754
+ $ this ->requestBuilder ->setFrom ($ searchCriteria ->getCurrentPage () * $ searchCriteria ->getPageSize ());
755
+ $ this ->requestBuilder ->setSize ($ searchCriteria ->getPageSize ());
756
+ $ request = $ this ->requestBuilder ->create ();
757
+ $ searchResponse = $ this ->searchEngine ->search ($ request );
758
+
759
+ return $ this ->searchResponseBuilder ->build ($ searchResponse )
760
+ ->setSearchCriteria ($ searchCriteria );
761
+ }
762
+
693
763
/**
694
764
* Helper function that adds a FilterGroup to the collection.
695
765
*
@@ -710,4 +780,26 @@ protected function addFilterGroupToCollection(
710
780
$ collection ->addFieldToFilter ($ fields );
711
781
}
712
782
}
783
+
784
+ /**
785
+ * Apply attribute filter to facet collection
786
+ *
787
+ * @param string $field
788
+ * @param null $condition
789
+ * @return $this
790
+ */
791
+ private function addFieldToFilter ($ field , $ condition = null )
792
+ {
793
+ if (!is_array ($ condition ) || !in_array (key ($ condition ), ['from ' , 'to ' ])) {
794
+ $ this ->requestBuilder ->bind ($ field , $ condition );
795
+ } else {
796
+ if (!empty ($ condition ['from ' ])) {
797
+ $ this ->requestBuilder ->bind ("{$ field }.from " , $ condition ['from ' ]);
798
+ }
799
+ if (!empty ($ condition ['to ' ])) {
800
+ $ this ->requestBuilder ->bind ("{$ field }.to " , $ condition ['to ' ]);
801
+ }
802
+ }
803
+ return $ this ;
804
+ }
713
805
}
0 commit comments