Skip to content

Commit 867c96d

Browse files
authored
Merge pull request #1299 from algolia/bugfix/MAGE-337
Instant Search Page Update
2 parents f669246 + 530dfb0 commit 867c96d

File tree

6 files changed

+71
-17
lines changed

6 files changed

+71
-17
lines changed

Block/Configuration.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function getConfiguration()
6969
$path = '';
7070
$level = '';
7171
$categoryId = '';
72+
$parentCategoryName = '';
7273

7374
$addToCartParams = $this->getAddToCartParams();
7475

@@ -94,6 +95,8 @@ public function getConfiguration()
9495
foreach ($category->getPathIds() as $treeCategoryId) {
9596
if ($path !== '') {
9697
$path .= ' /// ';
98+
}else{
99+
$parentCategoryName = $categoryHelper->getCategoryName($treeCategoryId, $this->getStoreId());
97100
}
98101

99102
$path .= $categoryHelper->getCategoryName($treeCategoryId, $this->getStoreId());
@@ -150,6 +153,7 @@ public function getConfiguration()
150153
'addToCartParams' => $addToCartParams,
151154
'infiniteScrollEnabled' => $config->isInfiniteScrollEnabled(),
152155
'urlTrackedParameters' => $this->getUrlTrackedParameters(),
156+
'isSearchBoxEnabled' => $config->isInstantSearchBoxEnabled(),
153157
],
154158
'autocomplete' => [
155159
'enabled' => $config->isAutoCompleteEnabled(),
@@ -228,6 +232,7 @@ public function getConfiguration()
228232
'landingPageId' => $this->getLandingPageId(),
229233
'path' => $path,
230234
'level' => $level,
235+
'parentCategory' => $parentCategoryName,
231236
],
232237
'showCatsNotIncludedInNavigation' => $config->showCatsNotIncludedInNavigation(),
233238
'showSuggestionsOnNoResultsPage' => $config->showSuggestionsOnNoResultsPage(),
@@ -307,6 +312,7 @@ public function getConfiguration()
307312
'searchBy' => __('Search by'),
308313
'searchForFacetValuesPlaceholder' => __('Search for other ...'),
309314
'showMore' => __('Show more products'),
315+
'searchTitle' => __('Search results for'),
310316
'placeholder' => __('Search for products, categories, ...'),
311317
'addToCart' => __('Add to Cart'),
312318
],
@@ -375,4 +381,4 @@ protected function getLandingPageConfiguration()
375381
{
376382
return $this->isLandingPage() ? $this->getCurrentLandingPage()->getConfiguration() : json_encode([]);
377383
}
378-
}
384+
}

Helper/ConfigHelper.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ConfigHelper
3232
public const SHOW_SUGGESTIONS_NO_RESULTS = 'algoliasearch_instant/instant/show_suggestions_on_no_result_page';
3333
public const XML_ADD_TO_CART_ENABLE = 'algoliasearch_instant/instant/add_to_cart_enable';
3434
public const INFINITE_SCROLL_ENABLE = 'algoliasearch_instant/instant/infinite_scroll_enable';
35+
public const SEARCHBOX_ENABLE = 'algoliasearch_instant/instant/instantsearch_searchbox';
3536

3637
public const IS_POPUP_ENABLED = 'algoliasearch_autocomplete/autocomplete/is_popup_enabled';
3738
public const NB_OF_PRODUCTS_SUGGESTIONS = 'algoliasearch_autocomplete/autocomplete/nb_of_products_suggestions';
@@ -538,6 +539,16 @@ public function isInfiniteScrollEnabled($storeId = null)
538539
&& $this->configInterface->isSetFlag(self::INFINITE_SCROLL_ENABLE, ScopeInterface::SCOPE_STORE, $storeId);
539540
}
540541

542+
/**
543+
* @param $storeId
544+
* @return bool
545+
*/
546+
public function isInstantSearchBoxEnabled($storeId = null)
547+
{
548+
return $this->isInstantEnabled($storeId)
549+
&& $this->configInterface->isSetFlag(self::SEARCHBOX_ENABLE, ScopeInterface::SCOPE_STORE, $storeId);
550+
}
551+
541552
/**
542553
* @param $storeId
543554
* @return bool
@@ -1603,4 +1614,4 @@ public function isAutocompleteNavigatorEnabled($storeId = null)
16031614
$storeId
16041615
);
16051616
}
1606-
}
1617+
}

etc/adminhtml/system.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,15 @@
345345
]]>
346346
</comment>
347347
</field>
348+
<field id="instantsearch_searchbox" translate="label comment" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
349+
<label>Enable searchBox Widget in Instant Search Page</label>
350+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
351+
<comment>
352+
<![CDATA[
353+
The searchBox widget is used to let the user perform a text-based query. <a target="_blank" href="https://www.algolia.com/doc/api-reference/widgets/search-box/js/#about"> Know More </a>
354+
]]>
355+
</comment>
356+
</field>
348357
<field id="replace_categories" translate="label comment" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
349358
<label>Replace categories page</label>
350359
<source_model>Magento\Config\Model\Config\Source\Yesno</source_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+
<instantsearch_searchbox>0</instantsearch_searchbox>
2122
<use_virtual_replica>0</use_virtual_replica>
2223
</instant>
2324
</algoliasearch_instant>

view/frontend/layout/catalogsearch_result_index.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0"?>
22
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
33
<body>
4+
<referenceBlock name="breadcrumbs" remove="true" />
45
<referenceBlock name="catalogsearch.leftnav">
56
<action ifconfig="algoliasearch_instant/instant/backend_rendering_enable" method="setTemplate">
67
<argument name="template" xsi:type="string">Algolia_AlgoliaSearch::layer/view.phtml</argument>

view/frontend/web/instantsearch.js

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,6 @@ requirejs(['algoliaBundle', 'Magento_Catalog/js/price-utils'], function (algolia
215215
}
216216
}
217217
],
218-
/**
219-
* searchBox
220-
* Docs: https://www.algolia.com/doc/api-reference/widgets/search-box/js/
221-
**/
222-
searchBox: {
223-
container: instant_selector,
224-
placeholder: algoliaConfig.translations.searchFor,
225-
showSubmit: false
226-
},
227218
/**
228219
* stats
229220
* Docs: https://www.algolia.com/doc/api-reference/widgets/stats/js/
@@ -267,7 +258,9 @@ requirejs(['algoliaBundle', 'Magento_Catalog/js/price-utils'], function (algolia
267258
item: $('#current-refinements-template').html()
268259
},
269260
includedAttributes: attributes.map(function (attribute) {
270-
return attribute.name
261+
if (!(algoliaConfig.isCategoryPage && attribute.name.indexOf('categories') > -1)) {
262+
return attribute.name;
263+
}
271264
}),
272265
transformItems: function (items) {
273266
return items.map(function (item) {
@@ -329,6 +322,24 @@ requirejs(['algoliaBundle', 'Magento_Catalog/js/price-utils'], function (algolia
329322
}
330323
};
331324

325+
if (algoliaConfig.instant.isSearchBoxEnabled){
326+
/**
327+
* searchBox
328+
* Docs: https://www.algolia.com/doc/api-reference/widgets/search-box/js/
329+
**/
330+
allWidgetConfiguration.searchBox = {
331+
container: instant_selector,
332+
placeholder: algoliaConfig.translations.searchFor,
333+
showSubmit: false,
334+
queryHook : function(inputValue, search) {
335+
if (algoliaConfig.isSearchPage && algoliaConfig.request.categoryId.length <= 0) {
336+
$(".page-title-wrapper span.base").html(algoliaConfig.translations.searchTitle+": '"+inputValue+"'");
337+
}
338+
return search(inputValue);
339+
}
340+
}
341+
}
342+
332343
if (algoliaConfig.instant.infiniteScrollEnabled === true) {
333344
/**
334345
* infiniteHits
@@ -421,15 +432,30 @@ requirejs(['algoliaBundle', 'Magento_Catalog/js/price-utils'], function (algolia
421432
attributes: hierarchical_levels,
422433
separator: ' /// ',
423434
templates: templates,
424-
alwaysGetRootLevel: true,
435+
alwaysGetRootLevel: false,
436+
showParentLevel:false,
425437
limit: algoliaConfig.maxValuesPerFacet,
426-
sortBy: ['name:asc']
438+
sortBy: ['name:asc'],
439+
transformItems(items) {
440+
if(algoliaConfig.isCategoryPage) {
441+
var filteredData = [];
442+
items.forEach(element => {
443+
if (element.label == algoliaConfig.request.parentCategory) {
444+
filteredData.push(element);
445+
};
446+
});
447+
items = filteredData;
448+
}
449+
return items.map(item => ({
450+
...item,
451+
label: item.label,
452+
}));
453+
},
427454
};
428455

429456
hierarchicalMenuParams.templates.item = '' +
430457
'<a class="{{cssClasses.link}} {{#isRefined}}{{cssClasses.link}}--selected{{/isRefined}}" href="{{url}}">{{label}}' + ' ' +
431-
'<span class="{{cssClasses.count}}">{{#helpers.formatNumber}}{{count}}{{/helpers.formatNumber}}</span>' + ' ' +
432-
'{{#isRefined}}<span class="cross-circle"></span>{{/isRefined}}' +
458+
'<span class="{{cssClasses.count}}">{{#helpers.formatNumber}}{{count}}{{/helpers.formatNumber}}</span>' +
433459
'</a>';
434460
hierarchicalMenuParams.panelOptions = {
435461
templates: {
@@ -644,4 +670,4 @@ requirejs(['algoliaBundle', 'Magento_Catalog/js/price-utils'], function (algolia
644670

645671
return options;
646672
}
647-
});
673+
});

0 commit comments

Comments
 (0)