Skip to content

Commit c0d54ba

Browse files
committed
MAGE-744 Add configuration for debounce and min char length on autocomplete
1 parent c1962d3 commit c0d54ba

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

Block/Configuration.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ public function getConfiguration()
164164
'nbOfCategoriesSuggestions' => $config->getNumberOfCategoriesSuggestions(),
165165
'nbOfQueriesSuggestions' => $config->getNumberOfQueriesSuggestions(),
166166
'isDebugEnabled' => $config->isAutocompleteDebugEnabled(),
167-
'isNavigatorEnabled' => $config->isAutocompleteNavigatorEnabled()
167+
'isNavigatorEnabled' => $config->isAutocompleteNavigatorEnabled(),
168+
'debounceMilliseconds' => $config->getAutocompleteDebounceMilliseconds(),
169+
'minimumCharacters' => $config->getAutocompleteMinimumCharacterLength()
168170
],
169171
'landingPage' => [
170172
'query' => $this->getLandingPageQuery(),

Helper/ConfigHelper.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class ConfigHelper
4545
public const MIN_NUMBER_OF_RESULTS = 'algoliasearch_autocomplete/autocomplete/min_number_of_results';
4646
public const RENDER_TEMPLATE_DIRECTIVES = 'algoliasearch_autocomplete/autocomplete/render_template_directives';
4747
public const AUTOCOMPLETE_MENU_DEBUG = 'algoliasearch_autocomplete/autocomplete/debug';
48+
public const AUTOCOMPLETE_DEBOUNCE_MILLIS = 'algoliasearch_autocomplete/autocomplete/debounce_millis';
49+
public const AUTOCOMPLETE_MINIMUM_CHAR_LENGTH = 'algoliasearch_autocomplete/autocomplete/minimum_char_length';
4850

4951
public const PRODUCT_ATTRIBUTES = 'algoliasearch_products/products/product_additional_attributes';
5052
public const PRODUCT_CUSTOM_RANKING = 'algoliasearch_products/products/custom_ranking_product_attributes';
@@ -933,6 +935,24 @@ public function isAutocompleteDebugEnabled($storeId = null)
933935
return $this->configInterface->isSetFlag(self::AUTOCOMPLETE_MENU_DEBUG, ScopeInterface::SCOPE_STORE, $storeId);
934936
}
935937

938+
public function getAutocompleteDebounceMilliseconds($storeId = null): int
939+
{
940+
return (int) $this->configInterface->getValue(
941+
self::AUTOCOMPLETE_DEBOUNCE_MILLIS,
942+
ScopeInterface::SCOPE_STORE,
943+
$storeId
944+
);
945+
}
946+
947+
public function getAutocompleteMinimumCharacterLength($storeId = null): int
948+
{
949+
return (int) $this->configInterface->getValue(
950+
self::AUTOCOMPLETE_MINIMUM_CHAR_LENGTH,
951+
ScopeInterface::SCOPE_STORE,
952+
$storeId
953+
);
954+
}
955+
936956
/**
937957
* @param $originalIndexName
938958
* @param $storeId

etc/adminhtml/system.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,30 @@
243243
<field id="is_popup_enabled">1</field>
244244
</depends>
245245
</field>
246+
<field id="debounce_millis" translate="label comment" type="text" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
247+
<validate>validate-digits</validate>
248+
<label>Debounce requests</label>
249+
<comment>
250+
<![CDATA[
251+
The minimal time (in milliseconds) that should elapse as users type their search query before a request is sent to the Algolia Search API. Default value is 300.
252+
]]>
253+
</comment>
254+
<depends>
255+
<field id="is_popup_enabled">1</field>
256+
</depends>
257+
</field>
258+
<field id="minimum_char_length" translate="label comment" type="text" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
259+
<validate>validate-digits</validate>
260+
<label>Minimum query length</label>
261+
<comment>
262+
<![CDATA[
263+
The minimal number of characters required for a search term before a request is sent to the Algolia Search API. If set to 0 then results will be fetched on first keystroke. Default value is 0.
264+
]]>
265+
</comment>
266+
<depends>
267+
<field id="is_popup_enabled">1</field>
268+
</depends>
269+
</field>
246270
</group>
247271
</section>
248272
<section id="algoliasearch_instant" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">

etc/config.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<sections><![CDATA[{"_1600351750374_374":{"name":"pages","label":"Pages","hitsPerPage":"2"}}]]></sections>
1313
<excluded_pages><![CDATA[{"_1600351757831_831":{"attribute":"no-route"}}]]></excluded_pages>
1414
<navigator>1</navigator>
15+
<debounce_millis>300</debounce_millis>
16+
<minimum_char_length>0</minimum_char_length>
1517
</autocomplete>
1618
</algoliasearch_autocomplete>
1719
<algoliasearch_instant>

view/frontend/web/autocomplete.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ define(
1515
function ($, algoliaBundle, pagesHtml, categoriesHtml, productsHtml, suggestionsHtml, additionalHtml) {
1616

1717
const DEFAULT_HITS_PER_SECTION = 2;
18-
const DEBOUNCE_MS = 300;
19-
const MIN_SEARCH_LENGTH_CHARS = 3;
18+
const DEBOUNCE_MS = algoliaConfig.autocomplete.debounceMilliseconds;
19+
const MIN_SEARCH_LENGTH_CHARS = algoliaConfig.autocomplete.minimumCharacters;
2020

2121
// global state
2222
let suggestionSection = false;

0 commit comments

Comments
 (0)