Skip to content

Commit

Permalink
Merge pull request #1453 from algolia/bugfix/MAGE-797
Browse files Browse the repository at this point in the history
Duplicate Replica Issue one set is standard and another is virtual
  • Loading branch information
mohitalgolia committed Jan 31, 2024
2 parents f0010a3 + f28b593 commit 1f4dc4b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 8 additions & 6 deletions Helper/Entity/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic

// Managing Virtual Replica
if ($this->configHelper->useVirtualReplica($storeId)) {
$replicas = $this->handleVirtualReplica($replicas, $indexName);
$replicas = $this->handleVirtualReplica($replicas);
}

// Merge current replicas with sorting replicas to not delete A/B testing replica indices
Expand Down Expand Up @@ -1534,7 +1534,7 @@ public function productIsInStock($product, $storeId)
* @param $replica
* @return array
*/
public function handleVirtualReplica($replicas, $indexName)
public function handleVirtualReplica($replicas)
{
$virtualReplicaArray = [];
foreach ($replicas as $replica) {
Expand All @@ -1558,17 +1558,19 @@ public function handlingReplica($indexName, $storeId, $sortingAttribute = false)
return $sortingIndex['name'];
}, $sortingIndices));
try {
$replicasFormated = $this->handleVirtualReplica($replicas);
$availableReplicaMatch = array_merge($replicasFormated, $replicas);
if ($this->configHelper->useVirtualReplica($storeId)) {
$replicas = $this->handleVirtualReplica($replicas, $indexName);
$replicas = $replicasFormated;
}
$currentSettings = $this->algoliaHelper->getSettings($indexName);
if (is_array($currentSettings) && array_key_exists('replicas', $currentSettings)) {
$replicasRequired = array_values(array_diff_assoc($currentSettings['replicas'], $replicas));
$replicasRequired = array_values(array_diff($currentSettings['replicas'], $availableReplicaMatch));
$this->algoliaHelper->setSettings($indexName, ['replicas' => $replicasRequired]);
$setReplicasTaskId = $this->algoliaHelper->getLastTaskId();
$this->algoliaHelper->waitLastTask($indexName, $setReplicasTaskId);
if (count($replicas) > 0) {
foreach ($replicas as $replicaIndex) {
if (count($availableReplicaMatch) > 0) {
foreach ($availableReplicaMatch as $replicaIndex) {
$this->algoliaHelper->deleteIndex($replicaIndex);
}
}
Expand Down
4 changes: 3 additions & 1 deletion Model/Backend/Sorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ public function afterSave()
if ($this->isValueChanged()) {
try{
$oldValue = $this->serializer->unserialize($this->getOldValue());
$updatedValue = $this->serializer->unserialize($this->getValue());
$sortingAttributes = array_merge($oldValue, $updatedValue);
$storeIds = array_keys($this->storeManager->getStores());
foreach ($storeIds as $storeId) {
$indexName = $this->helper->getIndexName($this->productHelper->getIndexNameSuffix(), $storeId);
$this->productHelper->handlingReplica($indexName, $storeId, $oldValue);
$this->productHelper->handlingReplica($indexName, $storeId, $sortingAttributes);
}
} catch (AlgoliaException $e) {
if ($e->getCode() !== 404) {
Expand Down

0 comments on commit 1f4dc4b

Please sign in to comment.