Skip to content

Commit 2c28324

Browse files
authored
Merge pull request #1294 from algolia/feature/MAGE-464
Feature/mage 464
2 parents bc251ba + 8d8ee0e commit 2c28324

File tree

5 files changed

+108
-14
lines changed

5 files changed

+108
-14
lines changed

Helper/ConfigHelper.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class ConfigHelper
123123
protected const IS_ADDTOCART_ENABLED_IN_FREQUENTLY_BOUGHT_TOGETHER = 'algoliasearch_recommend/recommend/frequently_bought_together/is_addtocart_enabled';
124124
protected const IS_ADDTOCART_ENABLED_IN_RELATED_PRODUCTS = 'algoliasearch_recommend/recommend/related_product/is_addtocart_enabled';
125125
protected const IS_ADDTOCART_ENABLED_IN_TRENDS_ITEM = 'algoliasearch_recommend/recommend/trends_item/is_addtocart_enabled';
126+
protected const USE_VIRTUAL_REPLICA_ENABLED = 'algoliasearch_instant/instant/use_virtual_replica';
126127
protected const AUTOCOMPLETE_KEYBORAD_NAVIAGATION = 'algoliasearch_autocomplete/autocomplete/navigator';
127128
protected const FREQUENTLY_BOUGHT_TOGETHER_TITLE = 'algoliasearch_recommend/recommend/frequently_bought_together/title';
128129
protected const RELATED_PRODUCTS_TITLE = 'algoliasearch_recommend/recommend/related_product/title';
@@ -1643,14 +1644,25 @@ public function getCacheTime($storeId = null)
16431644
);
16441645
}
16451646

1647+
/**
1648+
* @param $storeId
1649+
* @return mixed
1650+
*/
1651+
public function useVirtualReplica($storeId = null) {
1652+
return $this->configInterface->isSetFlag(self::USE_VIRTUAL_REPLICA_ENABLED,
1653+
ScopeInterface::SCOPE_STORE,
1654+
$storeId
1655+
);
1656+
}
1657+
16461658
/**
16471659
* @param $storeId
16481660
* @return mixed
16491661
*/
16501662
public function isAutocompleteNavigatorEnabled($storeId = null)
16511663
{
16521664
return $this->configInterface->isSetFlag(self::AUTOCOMPLETE_KEYBORAD_NAVIAGATION,
1653-
ScopeInterface::SCOPE_STORE,
1665+
ScopeInterface::SCOPE_STORE,
16541666
$storeId
16551667
);
16561668
}

Helper/Entity/ProductHelper.php

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
423423
'index_settings' => $transport,
424424
]
425425
);
426+
426427
$indexSettings = $transport->getData();
427428

428429
$this->algoliaHelper->setSettings($indexName, $indexSettings, false, true);
@@ -450,6 +451,11 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
450451
}, $sortingIndices));
451452
}
452453

454+
// Managing Virtual Replica
455+
if ($this->configHelper->useVirtualReplica($storeId)) {
456+
$replicas = $this->handleVirtualReplica($replicas, $indexName);
457+
}
458+
453459
// Merge current replicas with sorting replicas to not delete A/B testing replica indices
454460
try {
455461
$currentSettings = $this->algoliaHelper->getSettings($indexName);
@@ -464,19 +470,26 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
464470

465471
if (count($replicas) > 0) {
466472
$this->algoliaHelper->setSettings($indexName, ['replicas' => $replicas]);
467-
468473
$this->logger->log('Setting replicas to "' . $indexName . '" index.');
469474
$this->logger->log('Replicas: ' . json_encode($replicas));
470475
$setReplicasTaskId = $this->algoliaHelper->getLastTaskId();
471476

472-
foreach ($sortingIndices as $values) {
473-
$replicaName = $values['name'];
474-
$indexSettings['ranking'] = $values['ranking'];
475-
476-
$this->algoliaHelper->setSettings($replicaName, $indexSettings, false, true);
477-
478-
$this->logger->log('Setting settings to "' . $replicaName . '" replica.');
479-
$this->logger->log('Settings: ' . json_encode($indexSettings));
477+
if (!$this->configHelper->useVirtualReplica($storeId)) {
478+
foreach ($sortingIndices as $values) {
479+
$replicaName = $values['name'];
480+
$indexSettings['ranking'] = $values['ranking'];
481+
$this->algoliaHelper->setSettings($replicaName, $indexSettings, false, true);
482+
$this->logger->log('Setting settings to "' . $replicaName . '" replica.');
483+
$this->logger->log('Settings: ' . json_encode($indexSettings));
484+
}
485+
} else {
486+
foreach ($sortingIndices as $values) {
487+
$replicaName = $values['name'];
488+
$replicaSetting['customRanking'] = [$values['ranking'][0], ...$customRanking];
489+
$this->algoliaHelper->setSettings($replicaName, $replicaSetting, false, false);
490+
$this->logger->log('Setting settings to "' . $replicaName . '" replica.');
491+
$this->logger->log('Settings: ' . json_encode($replicaSetting));
492+
}
480493
}
481494
} else {
482495
$this->algoliaHelper->setSettings($indexName, ['replicas' => []]);
@@ -1402,4 +1415,16 @@ public function productIsInStock($product, $storeId)
14021415

14031416
return $product->isSaleable() && $stockItem->getIsInStock();
14041417
}
1418+
1419+
/**
1420+
* @param $replica
1421+
* @return array
1422+
*/
1423+
protected function handleVirtualReplica($replicas, $indexName) {
1424+
$virtualReplicaArray = [];
1425+
foreach ($replicas as $replica) {
1426+
$virtualReplicaArray[] = 'virtual('.$replica.')';
1427+
}
1428+
return $virtualReplicaArray;
1429+
}
14051430
}

Model/Observer/SaveSettings.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Algolia\AlgoliaSearch\Model\Observer;
44

55
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
6+
use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
7+
use Algolia\AlgoliaSearch\Helper\Data;
8+
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
69
use Algolia\AlgoliaSearch\Model\IndicesConfigurator;
710
use Magento\Framework\Event\Observer;
811
use Magento\Framework\Event\ObserverInterface;
@@ -11,17 +14,45 @@
1114
class SaveSettings implements ObserverInterface
1215
{
1316
/** @var StoreManagerInterface */
14-
private $storeManager;
17+
protected $storeManager;
1518

1619
/** @var IndicesConfigurator */
17-
private $indicesConfigurator;
20+
protected $indicesConfigurator;
1821

22+
/**
23+
* @var AlgoliaHelper
24+
*/
25+
protected $algoliaHelper;
26+
27+
/**
28+
* @var AlgoliaHelper
29+
*/
30+
protected $helper;
31+
32+
/**
33+
* @var ProductHelper
34+
*/
35+
protected $productHelper;
36+
37+
/**
38+
* @param StoreManagerInterface $storeManager
39+
* @param IndicesConfigurator $indicesConfigurator
40+
* @param AlgoliaHelper $algoliaHelper
41+
* @param Data $helper
42+
* @param ProductHelper $productHelper
43+
*/
1944
public function __construct(
2045
StoreManagerInterface $storeManager,
21-
IndicesConfigurator $indicesConfigurator
46+
IndicesConfigurator $indicesConfigurator,
47+
AlgoliaHelper $algoliaHelper,
48+
Data $helper,
49+
ProductHelper $productHelper
2250
) {
2351
$this->storeManager = $storeManager;
2452
$this->indicesConfigurator = $indicesConfigurator;
53+
$this->algoliaHelper = $algoliaHelper;
54+
$this->helper = $helper;
55+
$this->productHelper = $productHelper;
2556
}
2657

2758
/**
@@ -32,7 +63,20 @@ public function __construct(
3263
public function execute(Observer $observer)
3364
{
3465
$storeIds = array_keys($this->storeManager->getStores());
35-
66+
foreach ($storeIds as $storeId) {
67+
$indexName = $this->helper->getIndexName($this->productHelper->getIndexNameSuffix(), $storeId);
68+
$currentSettings = $this->algoliaHelper->getSettings($indexName);
69+
if (array_key_exists('replicas', $currentSettings)) {
70+
$this->algoliaHelper->setSettings($indexName, ['replicas' => []]);
71+
$setReplicasTaskId = $this->algoliaHelper->getLastTaskId();
72+
$this->algoliaHelper->waitLastTask($indexName, $setReplicasTaskId);
73+
if (count($currentSettings['replicas']) > 0) {
74+
foreach ($currentSettings['replicas'] as $replicaIndex) {
75+
$this->algoliaHelper->deleteIndex($replicaIndex);
76+
}
77+
}
78+
}
79+
}
3680
foreach ($storeIds as $storeId) {
3781
$this->indicesConfigurator->saveConfigurationToAlgolia($storeId);
3882
}

etc/adminhtml/system.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@
321321
]]>
322322
</comment>
323323
</field>
324+
<field id="use_virtual_replica" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
325+
<label>Use Virtual Replica</label>
326+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
327+
<comment>
328+
<![CDATA[
329+
This feature is only applicable with premium plans
330+
]]>
331+
</comment>
332+
<depends>
333+
<field id="is_instant_enabled">1</field>
334+
</depends>
335+
</field>
324336
<field id="sorts" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
325337
<label>Sorts</label>
326338
<frontend_model>Algolia\AlgoliaSearch\Model\Source\Sorts</frontend_model>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<instant>
1919
<facets><![CDATA[{"_1458145454535_587":{"attribute":"price","type":"slider","label":"Price","searchable":"2","create_rule":"2"},"_2541608784525_123":{"attribute":"categories","type":"conjunctive","label":"Categories","searchable":"2","create_rule":"2"},"_3211608784535_456":{"attribute":"color","type":"disjunctive","label":"Colors","searchable":"1","create_rule":"2"}}]]></facets>
2020
<sorts><![CDATA[{"_4581608784535_789":{"attribute":"price","sort":"asc","sortLabel":"Lowest price"},"_2541555584535_585":{"attribute":"price","sort":"desc","sortLabel":"Highest price"},"_5581608784535_898":{"attribute":"created_at","sort":"desc","sortLabel":"Newest first"}}]]></sorts>
21+
<use_virtual_replica>0</use_virtual_replica>
2122
</instant>
2223
</algoliasearch_instant>
2324
<algoliasearch_products>

0 commit comments

Comments
 (0)