Skip to content

Commit e8f7f54

Browse files
committed
MAGE-975 Add configuration to enable dynamic facets (disabled by default)
1 parent 5d33f91 commit e8f7f54

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

Block/Configuration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ public function getConfiguration()
197197
'categorySeparator' => $config->getCategorySeparator(),
198198
'categoryPageIdAttribute' => $config->getCategoryPageIdAttributeName(),
199199
'isCategoryNavigationEnabled' => self::IS_CATEGORY_NAVIGATION_ENABLED,
200-
'hidePagination' => $config->hidePaginationInInstantSearchPage()
200+
'hidePagination' => $config->hidePaginationInInstantSearchPage(),
201+
'isDynamicFacetsEnabled' => $config->isDynamicFacetsEnabled()
201202
],
202203
'autocomplete' => [
203204
'enabled' => $config->isAutoCompleteEnabled(),

Helper/ConfigHelper.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class ConfigHelper
3838
public const NUMBER_OF_PRODUCT_RESULTS = 'algoliasearch_instant/instant/number_product_results';
3939
public const FACETS = 'algoliasearch_instant/instant_facets/facets';
4040
public const MAX_VALUES_PER_FACET = 'algoliasearch_instant/instant_facets/max_values_per_facet';
41+
public const ENABLE_DYNAMIC_FACETS = 'algoliasearch_instant/instant_facets/enable_dynamic_facets';
42+
4143
public const SORTING_INDICES = 'algoliasearch_instant/instant_sorts/sorts';
4244
public const SEARCHBOX_ENABLE = 'algoliasearch_instant/instant_options/instantsearch_searchbox';
4345
public const SHOW_SUGGESTIONS_NO_RESULTS = 'algoliasearch_instant/instant_options/show_suggestions_on_no_result_page';
@@ -1328,6 +1330,19 @@ public function getFacets($storeId = null)
13281330
return [];
13291331
}
13301332

1333+
/**
1334+
* @param int|null $storeId
1335+
* @return bool
1336+
*/
1337+
public function isDynamicFacetsEnabled(?int $storeId = null): bool
1338+
{
1339+
return $this->configInterface->isSetFlag(
1340+
self::ENABLE_DYNAMIC_FACETS,
1341+
ScopeInterface::SCOPE_STORE,
1342+
$storeId
1343+
);
1344+
}
1345+
13311346
/**
13321347
* @param $storeId
13331348
* @return array

etc/adminhtml/system.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,21 @@
434434
]]>
435435
</comment>
436436
</field>
437+
<field id="enable_dynamic_facets" translate="label comment" type="select" sortOrder="10" showInDefault="1"
438+
showInWebsite="1" showInStore="1">
439+
<label>Enable dynamic facets</label>
440+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
441+
<comment>
442+
<![CDATA[
443+
While the rendering of facets is configured based on your Magento admin settings, it is possible to override
444+
these settings via merchandising rules in Algolia. Enable "dynamic facets" to allow InstantSearch
445+
to render facets based on the rule conditions you define in either the Algolia Dashboard or Merchandising Studio.
446+
]]>
447+
</comment>
448+
<depends>
449+
<field id="is_instant_enabled">1</field>
450+
</depends>
451+
</field>
437452
<depends>
438453
<field id="algoliasearch_instant/instant/is_instant_enabled">1</field>
439454
</depends>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<instant_facets>
3232
<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>
3333
<max_values_per_facet>10</max_values_per_facet>
34+
<enable_dynamic_facets>0</enable_dynamic_facets>
3435
</instant_facets>
3536
<instant_sorts>
3637
<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>

view/frontend/web/js/instantsearch.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,20 @@ define([
208208

209209
const configuredWidget = widget(config);
210210

211-
if (algoliaConfig.instant.isDynamicWidgetsEnabled ?? true) { //TODO: Implement this config
212-
if (this.isDynamicWidgetsEligible(type)) {
213-
this.dynamicWidgets.push(configuredWidget);
214-
return;
215-
}
211+
if (algoliaConfig.instant.isDynamicFacetsEnabled && this.isDynamicFacetsEligible(type)) {
212+
this.dynamicWidgets.push(configuredWidget);
213+
return;
216214
}
217215

218216
search.addWidgets([configuredWidget]);
219217
},
220218

219+
/**
220+
* Assigns designated facets to InstantSearch dynamicWidgets
221+
*
222+
* Docs: https://www.algolia.com/doc/api-reference/widgets/dynamic-facets/js/
223+
* @param search
224+
*/
221225
initializeDynamicWidgets(search) {
222226
const { dynamicWidgets } = instantsearch.widgets;
223227
search.addWidget(
@@ -235,7 +239,7 @@ define([
235239
* @param widgetType
236240
* @returns {boolean}
237241
*/
238-
isDynamicWidgetsEligible(widgetType) {
242+
isDynamicFacetsEligible(widgetType) {
239243
return [
240244
'refinementList',
241245
'menu',
@@ -467,7 +471,7 @@ define([
467471
/**
468472
* Add all facet widgets to allWidgetConfiguration
469473
* This is dynamically driven by the Magento facet configuration
470-
* Invokes facet builder function by attribute or type (where attribute builders take precendence)
474+
* Invokes facet builder function by attribute or type (where attribute builders take precedence)
471475
* The builders are responsible for flushing out the widget configuration for each facet
472476
*
473477
* @param allWidgetConfiguration

0 commit comments

Comments
 (0)