Skip to content

Commit f8b8798

Browse files
authored
Merge pull request #1404 from algolia/feature/MAGE-681-NS-POV
NS POV
2 parents 58a428f + b2eb258 commit f8b8798

File tree

8 files changed

+436
-39
lines changed

8 files changed

+436
-39
lines changed

Helper/AlgoliaHelper.php

Lines changed: 198 additions & 19 deletions
Large diffs are not rendered by default.

Helper/ConfigHelper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,12 +980,16 @@ public function getAutocompleteMinimumCharacterLength($storeId = null): int
980980
* @param $originalIndexName
981981
* @param $storeId
982982
* @param $currentCustomerGroupId
983+
* @param $attrs
983984
* @return array
984985
* @throws Magento\Framework\Exception\NoSuchEntityException
985986
*/
986-
public function getSortingIndices($originalIndexName, $storeId = null, $currentCustomerGroupId = null)
987+
public function getSortingIndices($originalIndexName, $storeId = null, $currentCustomerGroupId = null, $attrs = null)
987988
{
988-
$attrs = $this->getSorting($storeId);
989+
if (!$attrs){
990+
$attrs = $this->getSorting($storeId);
991+
}
992+
989993
$currency = $this->getCurrencyCode($storeId);
990994
$attributesToAdd = [];
991995
foreach ($attrs as $key => $attr) {

Helper/Entity/ProductHelper.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,12 +1532,52 @@ public function productIsInStock($product, $storeId)
15321532
* @param $replica
15331533
* @return array
15341534
*/
1535-
protected function handleVirtualReplica($replicas, $indexName)
1535+
public function handleVirtualReplica($replicas, $indexName)
15361536
{
15371537
$virtualReplicaArray = [];
15381538
foreach ($replicas as $replica) {
15391539
$virtualReplicaArray[] = 'virtual(' . $replica . ')';
15401540
}
15411541
return $virtualReplicaArray;
15421542
}
1543+
1544+
/**
1545+
* @param $indexName
1546+
* @param $storeId
1547+
* @param $sortingAttribute
1548+
* @return void
1549+
* @throws AlgoliaException
1550+
* @throws \Magento\Framework\Exception\NoSuchEntityException
1551+
*/
1552+
public function handlingReplica($indexName, $storeId, $sortingAttribute = false) {
1553+
$sortingIndices = $this->configHelper->getSortingIndices($indexName, $storeId, null, $sortingAttribute);
1554+
if ($this->configHelper->isInstantEnabled($storeId)) {
1555+
$replicas = array_values(array_map(function ($sortingIndex) {
1556+
return $sortingIndex['name'];
1557+
}, $sortingIndices));
1558+
try {
1559+
if ($this->configHelper->useVirtualReplica($storeId)) {
1560+
$replicas = $this->handleVirtualReplica($replicas, $indexName);
1561+
}
1562+
$currentSettings = $this->algoliaHelper->getSettings($indexName);
1563+
if (is_array($currentSettings) && array_key_exists('replicas', $currentSettings)) {
1564+
$replicasRequired = array_values(array_diff_assoc($currentSettings['replicas'], $replicas));
1565+
$this->algoliaHelper->setSettings($indexName, ['replicas' => $replicasRequired]);
1566+
$setReplicasTaskId = $this->algoliaHelper->getLastTaskId();
1567+
$this->algoliaHelper->waitLastTask($indexName, $setReplicasTaskId);
1568+
if (count($replicas) > 0) {
1569+
foreach ($replicas as $replicaIndex) {
1570+
$this->algoliaHelper->deleteIndex($replicaIndex);
1571+
}
1572+
}
1573+
}
1574+
} catch (AlgoliaException $e) {
1575+
if ($e->getCode() !== 404) {
1576+
$this->logger->log($e->getMessage());
1577+
throw $e;
1578+
}
1579+
}
1580+
}
1581+
return true;
1582+
}
15431583
}

Model/Backend/Replica.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Model\Backend;
4+
5+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
6+
use Algolia\AlgoliaSearch\Helper\Data;
7+
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
8+
use Magento\Framework\App\Cache\TypeListInterface;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\App\Config\Value;
11+
use Magento\Framework\Data\Collection\AbstractDb;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\Model\Context;
14+
use Magento\Framework\Model\ResourceModel\AbstractResource;
15+
use Magento\Framework\Registry;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
18+
class Replica extends Value
19+
{
20+
/** @var StoreManagerInterface */
21+
protected $storeManager;
22+
/**
23+
* @var Data
24+
*/
25+
protected $helper;
26+
27+
/**
28+
* @var ProductHelper
29+
*/
30+
protected $productHelper;
31+
32+
/**
33+
* @param Context $context
34+
* @param Registry $registry
35+
* @param ScopeConfigInterface $config
36+
* @param TypeListInterface $cacheTypeList
37+
* @param StoreManagerInterface $storeManager
38+
* @param Data $helper
39+
* @param ProductHelper $productHelper
40+
* @param AbstractResource|null $resource
41+
* @param AbstractDb|null $resourceCollection
42+
* @param array $data
43+
*/
44+
public function __construct(
45+
Context $context,
46+
Registry $registry,
47+
ScopeConfigInterface $config,
48+
TypeListInterface $cacheTypeList,
49+
StoreManagerInterface $storeManager,
50+
Data $helper,
51+
ProductHelper $productHelper,
52+
AbstractResource $resource = null,
53+
AbstractDb $resourceCollection = null,
54+
array $data = []
55+
) {
56+
$this->storeManager = $storeManager;
57+
$this->helper = $helper;
58+
$this->productHelper = $productHelper;
59+
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
60+
}
61+
62+
/**
63+
* @return $this
64+
* @throws AlgoliaException
65+
* @throws NoSuchEntityException
66+
*/
67+
public function afterSave()
68+
{
69+
if ($this->isValueChanged()) {
70+
try {
71+
$storeIds = array_keys($this->storeManager->getStores());
72+
foreach ($storeIds as $storeId) {
73+
$indexName = $this->helper->getIndexName($this->productHelper->getIndexNameSuffix(), $storeId);
74+
$this->productHelper->handlingReplica($indexName, $storeId);
75+
}
76+
} catch (AlgoliaException $e) {
77+
if ($e->getCode() !== 404) {
78+
throw $e;
79+
}
80+
}
81+
}
82+
return parent::afterSave();
83+
}
84+
}
85+

Model/Backend/Sorts.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Model\Backend;
4+
5+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
6+
use Algolia\AlgoliaSearch\Helper\Data;
7+
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
8+
use Magento\Config\Model\Config\Backend\Serialized\ArraySerialized;
9+
use Magento\Framework\App\Cache\TypeListInterface;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Data\Collection\AbstractDb;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\Model\Context;
15+
use Magento\Framework\Model\ResourceModel\AbstractResource;
16+
use Magento\Framework\Registry;
17+
use Magento\Framework\Serialize\Serializer\Json;
18+
use Magento\Store\Model\StoreManagerInterface;
19+
20+
class Sorts extends ArraySerialized
21+
{
22+
/** @var StoreManagerInterface */
23+
protected $storeManager;
24+
/**
25+
* @var Data
26+
*/
27+
protected $helper;
28+
29+
/**
30+
* @var ProductHelper
31+
*/
32+
protected $productHelper;
33+
34+
/**
35+
* @param Context $context
36+
* @param Registry $registry
37+
* @param ScopeConfigInterface $config
38+
* @param TypeListInterface $cacheTypeList
39+
* @param StoreManagerInterface $storeManager
40+
* @param Data $helper
41+
* @param ProductHelper $productHelper
42+
* @param AbstractResource|null $resource
43+
* @param AbstractDb|null $resourceCollection
44+
* @param array $data
45+
* @param Json|null $serializer
46+
*/
47+
public function __construct(
48+
Context $context,
49+
Registry $registry,
50+
ScopeConfigInterface $config,
51+
TypeListInterface $cacheTypeList,
52+
StoreManagerInterface $storeManager,
53+
Data $helper,
54+
ProductHelper $productHelper,
55+
AbstractResource $resource = null,
56+
AbstractDb $resourceCollection = null,
57+
array $data = [],
58+
Json $serializer = null
59+
) {
60+
$this->storeManager = $storeManager;
61+
$this->helper = $helper;
62+
$this->productHelper = $productHelper;
63+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
64+
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
65+
}
66+
67+
/**
68+
* @return $this
69+
* @throws AlgoliaException
70+
* @throws NoSuchEntityException
71+
*/
72+
public function afterSave()
73+
{
74+
if ($this->isValueChanged()) {
75+
try{
76+
$oldValue = $this->serializer->unserialize($this->getOldValue());
77+
$storeIds = array_keys($this->storeManager->getStores());
78+
foreach ($storeIds as $storeId) {
79+
$indexName = $this->helper->getIndexName($this->productHelper->getIndexNameSuffix(), $storeId);
80+
$this->productHelper->handlingReplica($indexName, $storeId, $oldValue);
81+
}
82+
} catch (AlgoliaException $e) {
83+
if ($e->getCode() !== 404) {
84+
throw $e;
85+
}
86+
}
87+
}
88+
return parent::afterSave();
89+
}
90+
}

Model/IndicesConfigurator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ class IndicesConfigurator
4242
/** @var Logger */
4343
protected $logger;
4444

45+
/**
46+
* @param Data $baseHelper
47+
* @param AlgoliaHelper $algoliaHelper
48+
* @param ConfigHelper $configHelper
49+
* @param ProductHelper $productHelper
50+
* @param CategoryHelper $categoryHelper
51+
* @param PageHelper $pageHelper
52+
* @param SuggestionHelper $suggestionHelper
53+
* @param AdditionalSectionHelper $additionalSectionHelper
54+
* @param Logger $logger
55+
*/
4556
public function __construct(
4657
Data $baseHelper,
4758
AlgoliaHelper $algoliaHelper,

Model/Observer/SaveSettings.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,15 @@ public function __construct(
6262
*/
6363
public function execute(Observer $observer)
6464
{
65-
try {
65+
try {
6666
$storeIds = array_keys($this->storeManager->getStores());
6767
foreach ($storeIds as $storeId) {
68-
$indexName = $this->helper->getIndexName($this->productHelper->getIndexNameSuffix(), $storeId);
69-
$currentSettings = $this->algoliaHelper->getSettings($indexName);
70-
if (is_array($currentSettings) && array_key_exists('replicas', $currentSettings)) {
71-
$this->algoliaHelper->setSettings($indexName, ['replicas' => []]);
72-
$setReplicasTaskId = $this->algoliaHelper->getLastTaskId();
73-
$this->algoliaHelper->waitLastTask($indexName, $setReplicasTaskId);
74-
if (count($currentSettings['replicas']) > 0) {
75-
foreach ($currentSettings['replicas'] as $replicaIndex) {
76-
$this->algoliaHelper->deleteIndex($replicaIndex);
77-
}
78-
}
79-
}
68+
$this->indicesConfigurator->saveConfigurationToAlgolia($storeId);
8069
}
8170
} catch (\Exception $e) {
8271
if ($e->getCode() !== 404) {
8372
throw $e;
8473
}
8574
}
86-
foreach ($storeIds as $storeId) {
87-
$this->indicesConfigurator->saveConfigurationToAlgolia($storeId);
88-
}
8975
}
9076
}

etc/adminhtml/system.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@
374374
<field id="use_virtual_replica" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
375375
<label>Use Virtual Replica</label>
376376
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
377+
<backend_model>Algolia\AlgoliaSearch\Model\Backend\Replica</backend_model>
377378
<comment>
378379
<![CDATA[
379380
Virtual Replica is only available on Premium plans <a target="_blank" href="https://www.algolia.com/doc/integration/magento-2/how-it-works/indexing/?client=php#switching-to-virtual-replica">documentation</a>
@@ -386,7 +387,7 @@
386387
<field id="sorts" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
387388
<label>Sorts</label>
388389
<frontend_model>Algolia\AlgoliaSearch\Model\Source\Sorts</frontend_model>
389-
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
390+
<backend_model>Algolia\AlgoliaSearch\Model\Backend\Sorts</backend_model>
390391
<comment>
391392
<![CDATA[
392393
Specify different sorting options you want to offer on a search results page.<br>
@@ -1136,6 +1137,7 @@
11361137
<field id="customer_groups_enable" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
11371138
<label>Enable Customer Groups</label>
11381139
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
1140+
<backend_model>Algolia\AlgoliaSearch\Model\Backend\Replica</backend_model>
11391141
<comment>
11401142
<![CDATA[
11411143
Do you want to take into account customer groups and display the price of the logged-in group instead of the default price?

0 commit comments

Comments
 (0)