Skip to content

Commit 15f8086

Browse files
authored
Merge pull request #1326 from algolia/feat/MAGE-579
Feat/mage 579
2 parents e241a15 + 5c235fc commit 15f8086

File tree

1 file changed

+63
-44
lines changed

1 file changed

+63
-44
lines changed

Helper/Entity/ProductHelper.php

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,24 @@ class ProductHelper
167167
* @param ImageHelper $imageHelper
168168
*/
169169
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,
178178
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+
{
187188
$this->eavConfig = $eavConfig;
188189
$this->configHelper = $configHelper;
189190
$this->algoliaHelper = $algoliaHelper;
@@ -295,7 +296,8 @@ public function getProductCollectionQuery(
295296
$productIds = null,
296297
$onlyVisible = true,
297298
$includeNotVisibleIndividually = false
298-
) {
299+
)
300+
{
299301
$productCollection = $this->productCollectionFactory->create();
300302
$products = $productCollection
301303
->setStoreId($storeId)
@@ -401,12 +403,12 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
401403
$attributesForFaceting = $this->getAttributesForFaceting($storeId);
402404

403405
$indexSettings = [
404-
'searchableAttributes' => $searchableAttributes,
405-
'customRanking' => $customRanking,
406+
'searchableAttributes' => $searchableAttributes,
407+
'customRanking' => $customRanking,
406408
'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),
410412
];
411413

412414
// Additional index settings from event observer
@@ -419,7 +421,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
419421
$this->eventManager->dispatch(
420422
'algolia_products_index_before_set_settings',
421423
[
422-
'store_id' => $storeId,
424+
'store_id' => $storeId,
423425
'index_settings' => $transport,
424426
]
425427
);
@@ -485,7 +487,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
485487
} else {
486488
foreach ($sortingIndices as $values) {
487489
$replicaName = $values['name'];
488-
array_unshift($customRanking,$values['ranking'][0]);
490+
array_unshift($customRanking, $values['ranking'][0]);
489491
$replicaSetting['customRanking'] = $customRanking;
490492
$this->algoliaHelper->setSettings($replicaName, $replicaSetting, false, false);
491493
$this->logger->log('Setting settings to "' . $replicaName . '" replica.');
@@ -578,12 +580,12 @@ public function getObject(Product $product)
578580
];
579581

580582
$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(),
587589
];
588590

589591
$additionalAttributes = $this->getAdditionalAttributes($product->getStoreId());
@@ -886,7 +888,7 @@ protected function addStockQty($defaultData, $customData, $additionalAttributes,
886888

887889
$stockItem = $this->stockRegistry->getStockItem($product->getId());
888890
if ($stockItem) {
889-
$customData['stock_qty'] = (int) $stockItem->getQty();
891+
$customData['stock_qty'] = (int)$stockItem->getQty();
890892
}
891893
}
892894

@@ -980,7 +982,7 @@ protected function addNullValue($customData, $subProducts, $attribute, Attribute
980982
}
981983

982984
if (is_array($values) && count($values) > 0) {
983-
$customData[$attributeName] = array_values(array_unique($values));
985+
$customData[$attributeName] = $this->getSanitizedArrayValues($values, $attributeName);
984986
}
985987

986988
if (count($subProductImages) > 0) {
@@ -991,12 +993,27 @@ protected function addNullValue($customData, $subProducts, $attribute, Attribute
991993
}
992994

993995
/**
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
9971014
* @return array
9981015
*/
999-
protected function getValues($valueText, Product $subProduct, AttributeResource $attributeResource)
1016+
protected function getValues($valueText, Product $subProduct, AttributeResource $attributeResource): array
10001017
{
10011018
$values = [];
10021019

@@ -1065,7 +1082,8 @@ protected function addNonNullValue(
10651082
Product $product,
10661083
$attribute,
10671084
AttributeResource $attributeResource
1068-
) {
1085+
)
1086+
{
10691087
$valueText = null;
10701088

10711089
if (!is_array($value) && $attributeResource->usesSource()) {
@@ -1165,7 +1183,7 @@ protected function getAttributesForFaceting($storeId)
11651183

11661184
if ($this->configHelper->isCustomerGroupsEnabled($storeId)) {
11671185
foreach ($this->groupCollection as $group) {
1168-
$group_id = (int) $group->getData('customer_group_id');
1186+
$group_id = (int)$group->getData('customer_group_id');
11691187

11701188
$attributesForFaceting[] = 'price.' . $currency_code . '.group_' . $group_id;
11711189
}
@@ -1338,10 +1356,10 @@ public function canProductBeReindexed($product, $storeId, $isChildProduct = fals
13381356
}
13391357

13401358
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+
])) {
13451363
throw (new ProductNotVisibleException())
13461364
->withProduct($product)
13471365
->withStoreId($storeId);
@@ -1381,10 +1399,11 @@ public function productIsInStock($product, $storeId)
13811399
* @param $replica
13821400
* @return array
13831401
*/
1384-
protected function handleVirtualReplica($replicas, $indexName) {
1402+
protected function handleVirtualReplica($replicas, $indexName)
1403+
{
13851404
$virtualReplicaArray = [];
13861405
foreach ($replicas as $replica) {
1387-
$virtualReplicaArray[] = 'virtual('.$replica.')';
1406+
$virtualReplicaArray[] = 'virtual(' . $replica . ')';
13881407
}
13891408
return $virtualReplicaArray;
13901409
}

0 commit comments

Comments
 (0)