Skip to content

Commit fb8fd29

Browse files
committed
Added option to select replica type per attribute
1 parent b272ab4 commit fb8fd29

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

Helper/ConfigHelper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,10 +989,16 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
989989
if (!$attrs){
990990
$attrs = $this->getSorting($storeId);
991991
}
992-
992+
993993
$currency = $this->getCurrencyCode($storeId);
994994
$attributesToAdd = [];
995+
$defaultVirtualReplicaEnabled = $this->useVirtualReplica($storeId);
995996
foreach ($attrs as $key => $attr) {
997+
if ($defaultVirtualReplicaEnabled || (isset($attr['virtualReplica']) && $attr['virtualReplica'])){
998+
$virtualReplica = 1;
999+
} else {
1000+
$virtualReplica = 0;
1001+
}
9961002
$indexName = false;
9971003
$sortAttribute = false;
9981004
if ($this->isCustomerGroupsEnabled($storeId) && $attr['attribute'] === 'price') {
@@ -1011,6 +1017,7 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10111017
$newAttr['attribute'] = $attr['attribute'];
10121018
$newAttr['sort'] = $attr['sort'];
10131019
$newAttr['sortLabel'] = $attr['sortLabel'];
1020+
$newAttr['virtualReplica'] = $virtualReplica;
10141021
if (!array_key_exists('label', $newAttr) && array_key_exists('sortLabel', $newAttr)) {
10151022
$newAttr['label'] = $newAttr['sortLabel'];
10161023
}
@@ -1036,6 +1043,7 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10361043
}
10371044
if ($indexName && $sortAttribute) {
10381045
$attrs[$key]['name'] = $indexName;
1046+
$attrs[$key]['virtualReplica'] = $virtualReplica;
10391047
if (!array_key_exists('label', $attrs[$key]) && array_key_exists('sortLabel', $attrs[$key])) {
10401048
$attrs[$key]['label'] = $attrs[$key]['sortLabel'];
10411049
}

Helper/Entity/ProductHelper.php

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -452,19 +452,16 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
452452
* Handle replicas
453453
*/
454454
$sortingIndices = $this->configHelper->getSortingIndices($indexName, $storeId);
455-
456455
$replicas = [];
457456

458457
if ($this->configHelper->isInstantEnabled($storeId)) {
459458
$replicas = array_values(array_map(function ($sortingIndex) {
460-
return $sortingIndex['name'];
459+
return [$sortingIndex['name'],$sortingIndex['virtualReplica']];
461460
}, $sortingIndices));
462461
}
463462

464463
// Managing Virtual Replica
465-
if ($this->configHelper->useVirtualReplica($storeId)) {
466-
$replicas = $this->handleVirtualReplica($replicas, $indexName);
467-
}
464+
$replicas = $this->handleVirtualReplica($replicas, $indexName, $storeId);
468465

469466
// Merge current replicas with sorting replicas to not delete A/B testing replica indices
470467
try {
@@ -483,25 +480,23 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
483480
$this->logger->log('Setting replicas to "' . $indexName . '" index.');
484481
$this->logger->log('Replicas: ' . json_encode($replicas));
485482
$setReplicasTaskId = $this->algoliaHelper->getLastTaskId();
486-
487-
if (!$this->configHelper->useVirtualReplica($storeId)) {
488-
foreach ($sortingIndices as $values) {
489-
$replicaName = $values['name'];
490-
$indexSettings['ranking'] = $values['ranking'];
491-
$this->algoliaHelper->setSettings($replicaName, $indexSettings, false, true);
492-
$this->logger->log('Setting settings to "' . $replicaName . '" replica.');
493-
$this->logger->log('Settings: ' . json_encode($indexSettings));
494-
}
495-
} else {
496-
foreach ($sortingIndices as $values) {
483+
foreach ($sortingIndices as $values) {
484+
if ($values['virtualReplica']) {
497485
$replicaName = $values['name'];
498486
array_unshift($customRanking, $values['ranking'][0]);
499487
$replicaSetting['customRanking'] = $customRanking;
500488
$this->algoliaHelper->setSettings($replicaName, $replicaSetting, false, false);
501489
$this->logger->log('Setting settings to "' . $replicaName . '" replica.');
502490
$this->logger->log('Settings: ' . json_encode($replicaSetting));
491+
} else {
492+
$replicaName = $values['name'];
493+
$indexSettings['ranking'] = $values['ranking'];
494+
$this->algoliaHelper->setSettings($replicaName, $indexSettings, false, true);
495+
$this->logger->log('Setting settings to "' . $replicaName . '" replica.');
496+
$this->logger->log('Settings: ' . json_encode($indexSettings));
503497
}
504498
}
499+
505500
} else {
506501
$this->algoliaHelper->setSettings($indexName, ['replicas' => []]);
507502
$this->logger->log('Removing replicas from "' . $indexName . '" index');
@@ -574,7 +569,6 @@ public function getObject(Product $product)
574569
);
575570

576571
$defaultData = $transport->getData();
577-
578572
$visibility = $product->getVisibility();
579573

580574
$visibleInCatalog = $this->visibility->getVisibleInCatalogIds();
@@ -1054,7 +1048,6 @@ protected function addAdditionalAttributes($customData, $additionalAttributes, P
10541048
}
10551049

10561050
$attributeResource = $attributeResource->setData('store_id', $product->getStoreId());
1057-
10581051
$value = $product->getData($attributeName);
10591052

10601053
if ($value !== null) {
@@ -1066,7 +1059,7 @@ protected function addAdditionalAttributes($customData, $additionalAttributes, P
10661059
}
10671060

10681061
$type = $product->getTypeId();
1069-
if ($type !== 'configurable' && $type !== 'grouped' && $type !== 'bundle') {
1062+
if ($type !== 'configurable' && $type !== 'grouped' && $type !== 'bundle' && $attributeName != 'test') {
10701063
continue;
10711064
}
10721065

@@ -1229,7 +1222,7 @@ protected function addNonNullValue(
12291222
$value = $attributeResource->getFrontend()->getValue($product);
12301223
}
12311224

1232-
if ($value) {
1225+
if ($value !== null) {
12331226
$customData[$attribute['attribute']] = $value;
12341227
}
12351228

@@ -1532,13 +1525,17 @@ public function productIsInStock($product, $storeId)
15321525
* @param $replica
15331526
* @return array
15341527
*/
1535-
public function handleVirtualReplica($replicas, $indexName)
1528+
public function handleVirtualReplica($replicas, $indexName, $storeId)
15361529
{
15371530
$virtualReplicaArray = [];
15381531
foreach ($replicas as $replica) {
1539-
$virtualReplicaArray[] = 'virtual(' . $replica . ')';
1532+
if ($replica[1]) {
1533+
$replicaArray[] = 'virtual(' . $replica[0] . ')';
1534+
} else {
1535+
$replicaArray[] = $replica[0];
1536+
}
15401537
}
1541-
return $virtualReplicaArray;
1538+
return $replicaArray;
15421539
}
15431540

15441541
/**
@@ -1553,12 +1550,12 @@ public function handlingReplica($indexName, $storeId, $sortingAttribute = false)
15531550
$sortingIndices = $this->configHelper->getSortingIndices($indexName, $storeId, null, $sortingAttribute);
15541551
if ($this->configHelper->isInstantEnabled($storeId)) {
15551552
$replicas = array_values(array_map(function ($sortingIndex) {
1556-
return $sortingIndex['name'];
1553+
return [$sortingIndex['name'],$sortingIndex['virtualReplica']];
15571554
}, $sortingIndices));
1555+
15581556
try {
1559-
if ($this->configHelper->useVirtualReplica($storeId)) {
1560-
$replicas = $this->handleVirtualReplica($replicas, $indexName);
1561-
}
1557+
$replicas = $this->handleVirtualReplica($replicas, $indexName, $storeId);
1558+
15621559
$currentSettings = $this->algoliaHelper->getSettings($indexName);
15631560
if (is_array($currentSettings) && array_key_exists('replicas', $currentSettings)) {
15641561
$replicasRequired = array_values(array_diff_assoc($currentSettings['replicas'], $replicas));
@@ -1580,4 +1577,4 @@ public function handlingReplica($indexName, $storeId, $sortingAttribute = false)
15801577
}
15811578
return true;
15821579
}
1583-
}
1580+
}

Model/Source/Sorts.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ protected function getTableData()
3333
'sortLabel' => [
3434
'label' => 'Label',
3535
],
36+
'virtualReplica' => [
37+
'label' => 'Enable Virtual Replica?',
38+
'values' => ['0' => __('No'), '1' => __('Yes')],
39+
],
3640
];
3741
}
3842
}

0 commit comments

Comments
 (0)