Skip to content

Instantly share code, notes, and snippets.

@damcou

damcou/hooks.js Secret

Last active March 20, 2019 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damcou/cabab5db4c3c1bc956b225dcd372e85c to your computer and use it in GitHub Desktop.
Save damcou/cabab5db4c3c1bc956b225dcd372e85c to your computer and use it in GitHub Desktop.
Frontend hook implementation to replace Magento suggestions by Algolia's Query Suggestion in the Autocomplete menu
/**
* Documentation: https://community.algolia.com/magento/doc/m2/frontend-events/
**/
/**
* Autocomplete hook method
* autocomplete.js documentation: https://github.com/algolia/autocomplete.js
**/
algolia.registerHook('beforeAutocompleteSources', function(sources, algoliaClient, algoliaBundle) {
// Parsing the different autocomplete source to find the suggestions one
for (var i = 0; i < sources.length; i++) {
if (sources[i].name === 'suggestions') {
// Setting the new index containing the Algolia Query Suggestions
var index = algoliaClient.initIndex('query_suggestions_test'),
suggestionsSource = algoliaBundle.$.fn.autocomplete.sources.hits(index, {
hitsPerPage: algoliaConfig.autocomplete.nbOfQueriesSuggestions
});
// Replacing the data for the suggestions source
sources[i] = {
source: suggestionsSource,
displayKey: 'query',
name: 'suggestions',
templates: {
suggestion: function (hit) {
hit.url = algoliaConfig.baseUrl + '/catalogsearch/result/?q=' + hit.query;
return algoliaConfig.autocomplete.templates.suggestions.render(hit);
}
}
};
break;
}
}
return sources;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment