@@ -167,23 +167,24 @@ class ProductHelper
167
167
* @param ImageHelper $imageHelper
168
168
*/
169
169
public function __construct (
170
- Config $ eavConfig ,
171
- ConfigHelper $ configHelper ,
172
- AlgoliaHelper $ algoliaHelper ,
173
- Logger $ logger ,
174
- StoreManagerInterface $ storeManager ,
175
- ManagerInterface $ eventManager ,
176
- Visibility $ visibility ,
177
- Stock $ stockHelper ,
170
+ Config $ eavConfig ,
171
+ ConfigHelper $ configHelper ,
172
+ AlgoliaHelper $ algoliaHelper ,
173
+ Logger $ logger ,
174
+ StoreManagerInterface $ storeManager ,
175
+ ManagerInterface $ eventManager ,
176
+ Visibility $ visibility ,
177
+ Stock $ stockHelper ,
178
178
StockRegistryInterface $ stockRegistry ,
179
- CurrencyHelper $ currencyManager ,
180
- CategoryHelper $ categoryHelper ,
181
- PriceManager $ priceManager ,
182
- Type $ productType ,
183
- CollectionFactory $ productCollectionFactory ,
184
- GroupCollection $ groupCollection ,
185
- ImageHelper $ imageHelper
186
- ) {
179
+ CurrencyHelper $ currencyManager ,
180
+ CategoryHelper $ categoryHelper ,
181
+ PriceManager $ priceManager ,
182
+ Type $ productType ,
183
+ CollectionFactory $ productCollectionFactory ,
184
+ GroupCollection $ groupCollection ,
185
+ ImageHelper $ imageHelper
186
+ )
187
+ {
187
188
$ this ->eavConfig = $ eavConfig ;
188
189
$ this ->configHelper = $ configHelper ;
189
190
$ this ->algoliaHelper = $ algoliaHelper ;
@@ -295,7 +296,8 @@ public function getProductCollectionQuery(
295
296
$ productIds = null ,
296
297
$ onlyVisible = true ,
297
298
$ includeNotVisibleIndividually = false
298
- ) {
299
+ )
300
+ {
299
301
$ productCollection = $ this ->productCollectionFactory ->create ();
300
302
$ products = $ productCollection
301
303
->setStoreId ($ storeId )
@@ -401,12 +403,12 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
401
403
$ attributesForFaceting = $ this ->getAttributesForFaceting ($ storeId );
402
404
403
405
$ indexSettings = [
404
- 'searchableAttributes ' => $ searchableAttributes ,
405
- 'customRanking ' => $ customRanking ,
406
+ 'searchableAttributes ' => $ searchableAttributes ,
407
+ 'customRanking ' => $ customRanking ,
406
408
'unretrievableAttributes ' => $ unretrievableAttributes ,
407
- 'attributesForFaceting ' => $ attributesForFaceting ,
408
- 'maxValuesPerFacet ' => (int ) $ this ->configHelper ->getMaxValuesPerFacet ($ storeId ),
409
- 'removeWordsIfNoResults ' => $ this ->configHelper ->getRemoveWordsIfNoResult ($ storeId ),
409
+ 'attributesForFaceting ' => $ attributesForFaceting ,
410
+ 'maxValuesPerFacet ' => (int )$ this ->configHelper ->getMaxValuesPerFacet ($ storeId ),
411
+ 'removeWordsIfNoResults ' => $ this ->configHelper ->getRemoveWordsIfNoResult ($ storeId ),
410
412
];
411
413
412
414
// Additional index settings from event observer
@@ -419,7 +421,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
419
421
$ this ->eventManager ->dispatch (
420
422
'algolia_products_index_before_set_settings ' ,
421
423
[
422
- 'store_id ' => $ storeId ,
424
+ 'store_id ' => $ storeId ,
423
425
'index_settings ' => $ transport ,
424
426
]
425
427
);
@@ -485,7 +487,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
485
487
} else {
486
488
foreach ($ sortingIndices as $ values ) {
487
489
$ replicaName = $ values ['name ' ];
488
- array_unshift ($ customRanking ,$ values ['ranking ' ][0 ]);
490
+ array_unshift ($ customRanking , $ values ['ranking ' ][0 ]);
489
491
$ replicaSetting ['customRanking ' ] = $ customRanking ;
490
492
$ this ->algoliaHelper ->setSettings ($ replicaName , $ replicaSetting , false , false );
491
493
$ this ->logger ->log ('Setting settings to " ' . $ replicaName . '" replica. ' );
@@ -578,12 +580,12 @@ public function getObject(Product $product)
578
580
];
579
581
580
582
$ customData = [
581
- 'objectID ' => $ product ->getId (),
582
- 'name ' => $ product ->getName (),
583
- 'url ' => $ product ->getUrlModel ()->getUrl ($ product , $ urlParams ),
584
- 'visibility_search ' => (int ) (in_array ($ visibility , $ visibleInSearch )),
585
- 'visibility_catalog ' => (int ) (in_array ($ visibility , $ visibleInCatalog )),
586
- 'type_id ' => $ product ->getTypeId (),
583
+ 'objectID ' => $ product ->getId (),
584
+ 'name ' => $ product ->getName (),
585
+ 'url ' => $ product ->getUrlModel ()->getUrl ($ product , $ urlParams ),
586
+ 'visibility_search ' => (int )(in_array ($ visibility , $ visibleInSearch )),
587
+ 'visibility_catalog ' => (int )(in_array ($ visibility , $ visibleInCatalog )),
588
+ 'type_id ' => $ product ->getTypeId (),
587
589
];
588
590
589
591
$ additionalAttributes = $ this ->getAdditionalAttributes ($ product ->getStoreId ());
@@ -886,7 +888,7 @@ protected function addStockQty($defaultData, $customData, $additionalAttributes,
886
888
887
889
$ stockItem = $ this ->stockRegistry ->getStockItem ($ product ->getId ());
888
890
if ($ stockItem ) {
889
- $ customData ['stock_qty ' ] = (int ) $ stockItem ->getQty ();
891
+ $ customData ['stock_qty ' ] = (int )$ stockItem ->getQty ();
890
892
}
891
893
}
892
894
@@ -980,7 +982,7 @@ protected function addNullValue($customData, $subProducts, $attribute, Attribute
980
982
}
981
983
982
984
if (is_array ($ values ) && count ($ values ) > 0 ) {
983
- $ customData [$ attributeName ] = array_values ( array_unique ( $ values) );
985
+ $ customData [$ attributeName ] = $ this -> getSanitizedArrayValues ( $ values, $ attributeName );
984
986
}
985
987
986
988
if (count ($ subProductImages ) > 0 ) {
@@ -991,12 +993,27 @@ protected function addNullValue($customData, $subProducts, $attribute, Attribute
991
993
}
992
994
993
995
/**
994
- * @param $valueText
995
- * @param Product $subProduct
996
- * @param AttributeResource $attributeResource
996
+ * By default Algolia will remove all redundant attribute values that are fetched from the child simple products.
997
+ *
998
+ * Overridable via Preference to allow implementer to enforce their own uniqueness rules while leveraging existing indexing code.
999
+ * e.g. $values = (in_array($attributeName, self::NON_UNIQUE_ATTRIBUTES)) ? $values : array_unique($values);
1000
+ *
1001
+ * @param array $values
1002
+ * @param string $attributeName
1003
+ * @return array
1004
+ */
1005
+ protected function getSanitizedArrayValues (array $ values , string $ attributeName ): array
1006
+ {
1007
+ return array_values (array_unique ($ values ));
1008
+ }
1009
+
1010
+ /**
1011
+ * @param string|array $valueText - bit of a misnomer - essentially the retrieved values to be indexed for a given product's attribute
1012
+ * @param Product $subProduct - the simple product to index
1013
+ * @param AttributeResource $attributeResource - the attribute being indexed
997
1014
* @return array
998
1015
*/
999
- protected function getValues ($ valueText , Product $ subProduct , AttributeResource $ attributeResource )
1016
+ protected function getValues ($ valueText , Product $ subProduct , AttributeResource $ attributeResource ): array
1000
1017
{
1001
1018
$ values = [];
1002
1019
@@ -1065,7 +1082,8 @@ protected function addNonNullValue(
1065
1082
Product $ product ,
1066
1083
$ attribute ,
1067
1084
AttributeResource $ attributeResource
1068
- ) {
1085
+ )
1086
+ {
1069
1087
$ valueText = null ;
1070
1088
1071
1089
if (!is_array ($ value ) && $ attributeResource ->usesSource ()) {
@@ -1165,7 +1183,7 @@ protected function getAttributesForFaceting($storeId)
1165
1183
1166
1184
if ($ this ->configHelper ->isCustomerGroupsEnabled ($ storeId )) {
1167
1185
foreach ($ this ->groupCollection as $ group ) {
1168
- $ group_id = (int ) $ group ->getData ('customer_group_id ' );
1186
+ $ group_id = (int )$ group ->getData ('customer_group_id ' );
1169
1187
1170
1188
$ attributesForFaceting [] = 'price. ' . $ currency_code . '.group_ ' . $ group_id ;
1171
1189
}
@@ -1338,10 +1356,10 @@ public function canProductBeReindexed($product, $storeId, $isChildProduct = fals
1338
1356
}
1339
1357
1340
1358
if ($ isChildProduct === false && !in_array ($ product ->getVisibility (), [
1341
- Visibility::VISIBILITY_BOTH ,
1342
- Visibility::VISIBILITY_IN_SEARCH ,
1343
- Visibility::VISIBILITY_IN_CATALOG ,
1344
- ])) {
1359
+ Visibility::VISIBILITY_BOTH ,
1360
+ Visibility::VISIBILITY_IN_SEARCH ,
1361
+ Visibility::VISIBILITY_IN_CATALOG ,
1362
+ ])) {
1345
1363
throw (new ProductNotVisibleException ())
1346
1364
->withProduct ($ product )
1347
1365
->withStoreId ($ storeId );
@@ -1381,10 +1399,11 @@ public function productIsInStock($product, $storeId)
1381
1399
* @param $replica
1382
1400
* @return array
1383
1401
*/
1384
- protected function handleVirtualReplica ($ replicas , $ indexName ) {
1402
+ protected function handleVirtualReplica ($ replicas , $ indexName )
1403
+ {
1385
1404
$ virtualReplicaArray = [];
1386
1405
foreach ($ replicas as $ replica ) {
1387
- $ virtualReplicaArray [] = 'virtual( ' . $ replica. ') ' ;
1406
+ $ virtualReplicaArray [] = 'virtual( ' . $ replica . ') ' ;
1388
1407
}
1389
1408
return $ virtualReplicaArray ;
1390
1409
}
0 commit comments