Skip to content

Commit f30d99c

Browse files
damcouJan Petr
authored andcommitted
Search for facet values (#1020)
1 parent a96d6b1 commit f30d99c

File tree

9 files changed

+87
-8
lines changed

9 files changed

+87
-8
lines changed

app/code/community/Algolia/Algoliasearch/Block/System/Config/Form/Field/Facets.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public function __construct()
4141
'label' => 'Label',
4242
'style' => 'width: 100px;',
4343
),
44+
'searchable' => array(
45+
'label' => 'Searchable?',
46+
'options' => array(
47+
'1' => 'Yes',
48+
'2' => 'No'
49+
),
50+
'rowMethod' => 'getSearchable',
51+
),
4452
),
4553
'buttonLabel' => 'Add Facet',
4654
'addAfter' => false,

app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,12 @@ public function setSettings($storeId, $saveToTmpIndicesToo = false)
253253
$attributesForFaceting[] = $facet['attribute'];
254254
}
255255
} else {
256-
$attributesForFaceting[] = $facet['attribute'];
256+
$attribute = $facet['attribute'];
257+
if (array_key_exists('searchable', $facet) && $facet['searchable'] === '1') {
258+
$attribute = 'searchable('.$attribute.')';
259+
}
260+
261+
$attributesForFaceting[] = $attribute;
257262
}
258263
}
259264

app/code/community/Algolia/Algoliasearch/etc/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@
200200
<instant>
201201
<replace_categories>1</replace_categories>
202202
<instant_selector>.main</instant_selector>
203-
<facets>a:3:{s:18:"_1432907948596_596";a:3:{s:9:"attribute";s:5:"price";s:4:"type";s:6:"slider";s:5:"label";s:5:"Price";}s:18:"_1432907963376_376";a:3:{s:9:"attribute";s:10:"categories";s:4:"type";s:11:"conjunctive";s:5:"label";s:10:"Categories";}s:17:"_1447846054032_32";a:3:{s:9:"attribute";s:5:"color";s:4:"type";s:11:"disjunctive";s:5:"label";s:6:"Colors";}}</facets>
203+
<facets>a:3:{s:18:"_1432907948596_596";a:4:{s:9:"attribute";s:5:"price";s:4:"type";s:6:"slider";s:5:"label";s:5:"Price";s:10:"searchable";s:1:"2";}s:18:"_1432907963376_376";a:4:{s:9:"attribute";s:10:"categories";s:4:"type";s:11:"conjunctive";s:5:"label";s:10:"Categories";s:10:"searchable";s:1:"2";}s:17:"_1447846054032_32";a:4:{s:9:"attribute";s:5:"color";s:4:"type";s:11:"disjunctive";s:5:"label";s:6:"Colors";s:10:"searchable";s:1:"1";}}</facets>
204204
<max_values_per_facet>10</max_values_per_facet>
205205
<sorts>a:3:{s:18:"_1432908018844_844";a:3:{s:9:"attribute";s:5:"price";s:4:"sort";s:3:"asc";s:5:"label";s:12:"Lowest price";}s:18:"_1432908022539_539";a:3:{s:9:"attribute";s:5:"price";s:4:"sort";s:4:"desc";s:5:"label";s:13:"Highest price";}s:18:"_1433768597454_454";a:3:{s:9:"attribute";s:10:"created_at";s:4:"sort";s:4:"desc";s:5:"label";s:12:"Newest first";}}</sorts>
206206
<add_to_cart_enable>1</add_to_cart_enable>

app/code/community/Algolia/Algoliasearch/etc/system.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,10 @@
402402
<![CDATA[
403403
Configure here the filtering options you want to display on your search results page.
404404
Choose Disjunctive to allow the selection of different values of a facet (e.g. hotels with 4 OR 5 stars).
405-
Choose Conjunctive to allow the selection of only one value of a facet (e.g. only "Size M").<br>
405+
Choose Conjunctive to allow the selection of only one value of a facet (e.g. only "Size M").
406+
<br>
407+
Set facet as searchable if you want your customers to be able to <a target="_blank" href="https://www.algolia.com/doc/api-reference/api-methods/search-for-facet-values/">search for facet values</a>. It's handy when a facet has a lot of values.
408+
<br>
406409
Facets' documentation: <a target="_blank" href="https://community.algolia.com/magento/doc/m1/indexing/?utm_source=magento&utm_medium=extension&utm_campaign=magento_1&utm_term=shop-owner&utm_content=doc-link#facets">https://community.algolia.com/magento/doc/m1/indexing/#facets</a>
407410
]]>
408411
</comment>

app/design/frontend/base/default/template/algoliasearch/internals/configuration.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ $algoliaJsConfig = array(
280280
'products' => $this->__('Products'),
281281
'searchBy' => $this->__('Search by'),
282282
'showMore' => $this->__('Show more products'),
283+
'searchForFacetValuesPlaceholder' => $this->__('Search for other ...'),
283284
),
284285
);
285286

app/locale/en_US/Algolia_Algoliasearch.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,5 @@
7070
"Input", "Input"
7171
"Synonyms", "Synonyms"
7272
"One-way Synonyms", "One-way Synonyms"
73-
"Synonyms File", "Synonyms File"
73+
"Synonyms File", "Synonyms File"
74+
"Search for other ...","Search for other ..."

js/algoliasearch/instantsearch.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ document.addEventListener("DOMContentLoaded", function (event) {
384384
}
385385

386386
if (facet.type === 'conjunctive') {
387-
return ['refinementList', {
387+
var refinementListOptions = {
388388
container: facet.wrapper.appendChild(createISWidgetContainer(facet.attribute)),
389389
attributeName: facet.attribute,
390390
limit: algoliaConfig.maxValuesPerFacet,
@@ -393,11 +393,14 @@ document.addEventListener("DOMContentLoaded", function (event) {
393393
cssClasses: {
394394
root: 'facet conjunctive'
395395
}
396-
}];
396+
};
397+
398+
refinementListOptions = addSearchForFacetValues(facet, refinementListOptions);
399+
return ['refinementList', refinementListOptions];
397400
}
398401

399402
if (facet.type === 'disjunctive') {
400-
return ['refinementList', {
403+
var refinementListOptions = {
401404
container: facet.wrapper.appendChild(createISWidgetContainer(facet.attribute)),
402405
attributeName: facet.attribute,
403406
limit: algoliaConfig.maxValuesPerFacet,
@@ -406,7 +409,10 @@ document.addEventListener("DOMContentLoaded", function (event) {
406409
cssClasses: {
407410
root: 'facet disjunctive'
408411
}
409-
}];
412+
};
413+
414+
refinementListOptions = addSearchForFacetValues(facet, refinementListOptions);
415+
return ['refinementList', refinementListOptions];
410416
}
411417

412418
if (facet.type === 'slider') {
@@ -544,4 +550,17 @@ document.addEventListener("DOMContentLoaded", function (event) {
544550

545551
search.addWidget(algoliaBundle.instantsearch.widgets[type](config));
546552
}
553+
554+
function addSearchForFacetValues(facet, options) {
555+
if (facet.searchable === '1') {
556+
options['searchForFacetValues'] = {
557+
placeholder: algoliaConfig.translations.searchForFacetValuesPlaceholder,
558+
templates: {
559+
noResults: '<div class="sffv-no-results">' + algoliaConfig.translations.noResults + '</div>'
560+
}
561+
};
562+
}
563+
564+
return options;
565+
}
547566
});

skin/frontend/base/default/algoliasearch/algoliasearch.css

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,46 @@ a.ais-current-refined-values--link:hover
213213
color: #666;
214214
}
215215

216+
#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__wrapper {
217+
width: 94%;
218+
margin: 0 auto 5px;
219+
}
220+
221+
#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__submit {
222+
display: none;
223+
}
224+
225+
#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__reset {
226+
width: 10px;
227+
margin-top: -30px;
228+
margin-right: 7px;
229+
background: none;
230+
border: none;
231+
box-shadow: none;
232+
}
233+
234+
#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__reset:hover {
235+
background: none;
236+
}
237+
238+
#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__reset svg {
239+
width: 15px;
240+
height: 15px;
241+
fill: #636363;
242+
}
243+
244+
#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__input {
245+
border : 1px solid silver;
246+
}
247+
248+
#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__input:focus {
249+
border : 1px solid #54A5CD;
250+
}
251+
252+
#algolia_instant_selector .ais-refinement-list--list .sffv-no-results {
253+
padding-left: 3%;
254+
}
255+
216256
/* RANGE SLIDER */
217257

218258
.ais-range-slider--target {

tests/ConfigTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public function testFacets()
4242
$attributesMatched++;
4343
} elseif ($facet['attribute'] === $indexFacet) {
4444
$attributesMatched++;
45+
} elseif ($facet['attribute'] === 'color' && 'searchable(color)' === $indexFacet) {
46+
$attributesMatched++;
4547
}
4648
}
4749
}

0 commit comments

Comments
 (0)