Skip to content

Commit 0693840

Browse files
authored
Merge pull request #1863 from algolia/feat/MAGE-1328-algolia-query-suggestions
MAGE-1328: Add Algolia Query Suggestions backend configuration
2 parents ad58e16 + 5b67ca1 commit 0693840

File tree

6 files changed

+215
-1
lines changed

6 files changed

+215
-1
lines changed

Block/Configuration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,17 @@ protected function getAutocompleteConfiguration(): array
371371
'sections' => $config->getAdditionalSections(),
372372
'nbOfProductsSuggestions' => $config->getNumberOfProductsSuggestions(),
373373
'nbOfCategoriesSuggestions' => $config->getNumberOfCategoriesSuggestions(),
374+
// SUGGESTIONS - START
375+
'areSuggestionsEnabled' => $config->areSuggestionsEnabled(),
376+
'suggestionsMode' => $config->getSuggestionsMode(),
377+
// Magento
378+
'showMagentoSuggestions' => $config->showMagentoSuggestions(),
374379
'nbOfQueriesSuggestions' => $config->getNumberOfQueriesSuggestions(),
380+
// Algolia
381+
'showAlgoliaSuggestions' => $config->showAlgoliaSuggestions(),
382+
'suggestionsIndexName' => $config->getSuggestionsIndexName(),
383+
'nbOfAlgoliaSuggestions' => $config->getNumberOfAlgoliaSuggestions(),
384+
// SUGGESTIONS - END
375385
'isDebugEnabled' => $config->isDebugEnabled(),
376386
'isNavigatorEnabled' => $config->isKeyboardNavigationEnabled(),
377387
'debounceMilliseconds' => $config->getDebounceMilliseconds(),

Helper/Configuration/AutocompleteHelper.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Helper\Configuration;
44

55
use Algolia\AlgoliaSearch\Service\Serializer;
6+
use Algolia\AlgoliaSearch\Model\Source\Suggestions;
67
use Magento\Framework\App\Config\ScopeConfigInterface;
78
use Magento\Store\Model\ScopeInterface;
89

@@ -13,9 +14,13 @@ class AutocompleteHelper
1314
public const ADDITIONAL_SECTIONS = 'algoliasearch_autocomplete/autocomplete/sections';
1415
public const NB_OF_PRODUCTS_SUGGESTIONS = 'algoliasearch_autocomplete/autocomplete/nb_of_products_suggestions';
1516
public const NB_OF_CATEGORIES_SUGGESTIONS = 'algoliasearch_autocomplete/autocomplete/nb_of_categories_suggestions';
17+
18+
public const SUGGESTIONS_MODE = 'algoliasearch_autocomplete/autocomplete/suggestions_mode';
1619
public const NB_OF_QUERIES_SUGGESTIONS = 'algoliasearch_autocomplete/autocomplete/nb_of_queries_suggestions';
1720
public const MIN_QUERY_POPULARITY = 'algoliasearch_autocomplete/autocomplete/min_popularity';
1821
public const MIN_QUERY_NUMBER_OF_RESULTS = 'algoliasearch_autocomplete/autocomplete/min_number_of_results';
22+
public const SUGGESTIONS_INDEX_NAME = 'algoliasearch_autocomplete/autocomplete/suggestions_index_name';
23+
public const NB_OF_ALGOLIA_SUGGESTIONS = 'algoliasearch_autocomplete/autocomplete/nb_of_algolia_suggestions';
1924

2025
public const EXCLUDED_PAGES = 'algoliasearch_autocomplete/autocomplete/excluded_pages';
2126
public const RENDER_TEMPLATE_DIRECTIVES = 'algoliasearch_autocomplete/autocomplete/render_template_directives';
@@ -79,6 +84,33 @@ public function getNumberOfCategoriesSuggestions(?int $storeId = null): int
7984
);
8085
}
8186

87+
public function areSuggestionsEnabled(?int $storeId = null): bool
88+
{
89+
return $this->getSuggestionsMode($storeId) > 0;
90+
}
91+
92+
public function showMagentoSuggestions(?int $storeId = null): bool
93+
{
94+
return $this->getSuggestionsMode($storeId) === Suggestions::SUGGESTIONS_MAGENTO
95+
&& $this->getNumberOfQueriesSuggestions($storeId) > 0;
96+
}
97+
98+
public function showAlgoliaSuggestions(?int $storeId = null): bool
99+
{
100+
return $this->getSuggestionsMode($storeId) === Suggestions::SUGGESTIONS_ALGOLIA
101+
&& $this->getSuggestionsIndexName($storeId) !== ''
102+
&& $this->getNumberOfAlgoliaSuggestions($storeId) > 0;
103+
}
104+
105+
public function getSuggestionsMode(?int $storeId = null): int
106+
{
107+
return (int) $this->configInterface->getValue(
108+
self::SUGGESTIONS_MODE,
109+
ScopeInterface::SCOPE_STORE,
110+
$storeId
111+
);
112+
}
113+
82114
public function getNumberOfQueriesSuggestions(?int $storeId = null): int
83115
{
84116
return (int) $this->configInterface->getValue(
@@ -108,6 +140,24 @@ public function getMinQueryNumberOfResults(?int $storeId = null): int
108140
);
109141
}
110142

143+
public function getSuggestionsIndexName(?int $storeId = null): string
144+
{
145+
return $this->configInterface->getValue(
146+
self::SUGGESTIONS_INDEX_NAME,
147+
ScopeInterface::SCOPE_STORE,
148+
$storeId
149+
);
150+
}
151+
152+
public function getNumberOfAlgoliaSuggestions(?int $storeId = null): int
153+
{
154+
return (int) $this->configInterface->getValue(
155+
self::NB_OF_ALGOLIA_SUGGESTIONS,
156+
ScopeInterface::SCOPE_STORE,
157+
$storeId
158+
);
159+
}
160+
111161
/**
112162
* Retrieve CMS pages to be excluded from the Autocomplete search
113163
* Also impacts what pages are indexed

Model/Source/Suggestions.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Model\Source;
4+
5+
use Magento\Framework\Data\OptionSourceInterface;
6+
7+
class Suggestions implements OptionSourceInterface
8+
{
9+
10+
public const SUGGESTIONS_DISABLED = 0;
11+
public const SUGGESTIONS_MAGENTO = 1;
12+
public const SUGGESTIONS_ALGOLIA = 2;
13+
14+
public function toOptionArray()
15+
{
16+
return [
17+
[
18+
'value' => self::SUGGESTIONS_DISABLED,
19+
'label' => __('No'),
20+
],
21+
[
22+
'value' => self::SUGGESTIONS_MAGENTO,
23+
'label' => __('Use Magento Search Queries (deprecated)'),
24+
],
25+
[
26+
'value' => self::SUGGESTIONS_ALGOLIA,
27+
'label' => __('Use Algolia Query Suggestions'),
28+
],
29+
];
30+
}
31+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Setup\Patch\Data;
4+
5+
use Algolia\AlgoliaSearch\Helper\Configuration\AutocompleteHelper;
6+
use Algolia\AlgoliaSearch\Model\Source\Suggestions;
7+
use Magento\Framework\Setup\ModuleDataSetupInterface;
8+
use Magento\Framework\Setup\Patch\DataPatchInterface;
9+
use Magento\Framework\Setup\Patch\PatchInterface;
10+
11+
class MigrateSuggestionsConfigPatch implements DataPatchInterface
12+
{
13+
public function __construct(
14+
protected ModuleDataSetupInterface $moduleDataSetup,
15+
) {}
16+
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function apply(): PatchInterface
21+
{
22+
$this->moduleDataSetup->getConnection()->startSetup();
23+
24+
$this->moveIndexingSettings();
25+
26+
$this->moduleDataSetup->getConnection()->endSetup();
27+
28+
return $this;
29+
}
30+
31+
/**
32+
* Migrate legacy configurations
33+
* @return void
34+
*/
35+
protected function moveIndexingSettings(): void
36+
{
37+
$connection = $this->moduleDataSetup->getConnection();
38+
$configDataTable = $this->moduleDataSetup->getTable('core_config_data');
39+
40+
// Get current number of configured Magento suggestions
41+
$whereConfigPathFrom = $connection->quoteInto('path = ?', AutocompleteHelper::NB_OF_QUERIES_SUGGESTIONS);
42+
$select = $connection->select()
43+
->from($configDataTable)
44+
->where('path = ?', AutocompleteHelper::NB_OF_QUERIES_SUGGESTIONS);
45+
$existingValues = $connection->fetchAll($select);
46+
47+
foreach ($existingValues as $item) {
48+
// If number of suggestions used to be superior to zero, this means that the feature was activated
49+
// So we automatically set the suggestion mode to "Magento"
50+
if ((int) $item['value'] > 0) {
51+
$connection->insertOnDuplicate(
52+
$configDataTable,
53+
[
54+
'scope' => $item['scope'],
55+
'scope_id' => $item['scope_id'],
56+
'path' => AutocompleteHelper::SUGGESTIONS_MODE,
57+
'value' => Suggestions::SUGGESTIONS_MAGENTO
58+
]
59+
);
60+
}
61+
}
62+
}
63+
64+
/**
65+
* @inheritDoc
66+
*/
67+
public static function getDependencies(): array
68+
{
69+
return [];
70+
}
71+
72+
/**
73+
* @inheritDoc
74+
*/
75+
public function getAliases(): array
76+
{
77+
return [];
78+
}
79+
}

etc/adminhtml/system.xml

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,19 @@
166166
<field id="is_popup_enabled">1</field>
167167
</depends>
168168
</field>
169-
<field id="nb_of_queries_suggestions" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
169+
<field id="suggestions_mode" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
170+
<label>Enable Suggestions</label>
171+
<source_model>Algolia\AlgoliaSearch\Model\Source\Suggestions</source_model>
172+
<comment>
173+
<![CDATA[
174+
Choose if you want to use Magento Suggestions (populated by the <code>search_query</code> table) or the Algolia Suggestions from your Algolia dashboard.
175+
]]>
176+
</comment>
177+
<depends>
178+
<field id="is_popup_enabled">1</field>
179+
</depends>
180+
</field>
181+
<field id="nb_of_queries_suggestions" translate="label comment" type="text" sortOrder="25" showInDefault="1" showInWebsite="1" showInStore="1">
170182
<validate>validate-digits</validate>
171183
<label>Number of queries</label>
172184
<comment>
@@ -177,6 +189,7 @@
177189
</comment>
178190
<depends>
179191
<field id="is_popup_enabled">1</field>
192+
<field id="suggestions_mode">1</field>
180193
</depends>
181194
</field>
182195
<field id="min_popularity" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
@@ -189,6 +202,7 @@
189202
</comment>
190203
<depends>
191204
<field id="is_popup_enabled">1</field>
205+
<field id="suggestions_mode">1</field>
192206
</depends>
193207
</field>
194208
<field id="min_number_of_results" translate="label comment" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
@@ -201,6 +215,33 @@
201215
</comment>
202216
<depends>
203217
<field id="is_popup_enabled">1</field>
218+
<field id="suggestions_mode">1</field>
219+
</depends>
220+
</field>
221+
<field id="suggestions_index_name" translate="label comment" type="text" sortOrder="45" showInDefault="1" showInWebsite="1" showInStore="1">
222+
<label>Search suggestions index name</label>
223+
<comment>
224+
<![CDATA[
225+
Specify the index name you have configured in Algolia to use for search suggestions.<br/>
226+
For more information, check the <a target="_blank" href="https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions">Algolia official documentation</a>
227+
]]>
228+
</comment>
229+
<depends>
230+
<field id="is_popup_enabled">1</field>
231+
<field id="suggestions_mode">2</field>
232+
</depends>
233+
</field>
234+
<field id="nb_of_algolia_suggestions" translate="label comment" type="text" sortOrder="47" showInDefault="1" showInWebsite="1" showInStore="1">
235+
<validate>validate-digits</validate>
236+
<label>Number of Algolia Suggestions</label>
237+
<comment>
238+
<![CDATA[
239+
How many query Algolia Suggestions do you want to display ? (Must be higher than 0)
240+
]]>
241+
</comment>
242+
<depends>
243+
<field id="is_popup_enabled">1</field>
244+
<field id="suggestions_mode">2</field>
204245
</depends>
205246
</field>
206247
<field id="sections" translate="label comment" type="select" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">

etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
<autocomplete_selector>.algolia-search-input</autocomplete_selector>
2828
<nb_of_products_suggestions>6</nb_of_products_suggestions>
2929
<nb_of_categories_suggestions>2</nb_of_categories_suggestions>
30+
<suggestions_mode>0</suggestions_mode>
3031
<nb_of_queries_suggestions>0</nb_of_queries_suggestions>
3132
<min_popularity>1000</min_popularity>
3233
<min_number_of_results>2</min_number_of_results>
34+
<suggestions_index_name>query_suggestions_test</suggestions_index_name>
35+
<nb_of_algolia_suggestions>2</nb_of_algolia_suggestions>
3336
<sections><![CDATA[{"_1600351750374_374":{"name":"pages","label":"Pages","hitsPerPage":"2"}}]]></sections>
3437
<excluded_pages><![CDATA[{"_1600351757831_831":{"attribute":"no-route"}}]]></excluded_pages>
3538
<render_template_directives>1</render_template_directives>

0 commit comments

Comments
 (0)