- Implementing widgets for your search experience like search input field, facets, sorting, and results
- Templates for your search UI like filters, results, and the toolbar
vendor/algolia/algoliasearch-magento-2/view/frontend
folder, which contains the templates, JavaScript, and style sheets in use.
Widgets
The InstantSearch UI consists of widgets, which have a predefined behavior and rendering. For example, the search input field is a widget, and the results is another widget. The extension defines a set of widgets for your InstantSearch UI based on your configuration settings in Stores > Configuration > Algolia Search > InstantSearch Results Page Here are some widgets that the extension leverages for its UI:searchBox
stats
sortBy
currentRefinements
queryRuleCustomData
infiniteHits
orhits
pagination
rangeInput
refinementList
rangeSlider

beforeWidgetInitialization
event provided by the extension.
JavaScript
allWidgetConfiguration
parameter has access to the set variable allWidgetConfiguration
.
This variable has all the InstantSearch widget configurations already created for you based on your extension settings.
Using this event, you can return a modified allWidgetConfiguration
for InstantSearch to render.
The following sections reviews key implementation concepts to help better understand what events to target when customizing InstantSearch.
Layered navigation or facets
Magento defines the filters on the left column as layered navigation. Algolia calls them facets. InstantSearch builds these facet filters with refinement widgets like: The extension uses the configuration for facets set in Stores > Configuration > Algolia Search > InstantSearch Results Page > Facets, to define which refinement widget to use. You can use thealgoliaConfig
global in your developer tools console to review the configurations for the implementation using algoliaConfig.facets
in your store front.

Product results or hits

hits
and infiniteHits
widgets display the product results from a search.
The extension can configure either options.
You can enable infiniteHits
in Stores > Configuration > Algolia Search > InstantSearch Results Page > Enable Infinite Scrolling?
Both widgets use the same template to render the item template:
view/frontend/templates/instant/hit.phtml
.
To change the rendering of the product result,
copy the template in your theme, and edit the copy instead of directly in the extension.
If you need to add complex display conditions to your product hits,
use the beforeWidgetInitialization
event hook to transform the hit before passing in the variable to the template.
These widgets have the option to transformItems
, which accepts a callback function.
As the extension already utilizes this parameter,
make sure that you run the already created function before yours. For example:
JavaScript
{{{exampleNewVariable}}}

beforeWidgetInitialization
frontend hook to add more complex logic for your product listing.
Custom events
The extension offers several events within the InstantSearch implementation:-
beforeInstantsearchInit(instantsearchOptions)
Changes defaultinstantsearch
options. -
beforeWidgetInitialization(allWidgetConfiguration)
Adds, removes, or updates any widget. -
beforeInstantsearchStart(search)
Changes theinstantsearch
instance before calling thestart()
method. -
afterInstantsearchStart(search)
Changes theinstantsearch
instance after calling thestart()
method. -
beforeFacetInitialization(builders)
Modifies the rendering logic for any facet.
All custom methods must return the manipulated first parameter.
Change search options
If you need to change the InstantSearch search options, whether changing the index name or tapping directly to InstantSearch events, you can use the event:beforeInstantsearchInit
Use this event to change the instantsearchOptions
option which accepts a callback function.
As the extension already utilizes this parameter,
make sure that you run the already created function before yours.
For example:
JavaScript
Change search parameters
The method to change thesearchParameters
for your search request depends on your extension version.
In version 1, change this parameter in search options.
In versions 2 and later, the configure
widget sets the search parameters.
This widget accepts an object of searchParameters
. For example:
configure
widget for you,
use beforeWidgetInitialization
to make any changes to it.
The example below shows how to change and add preconfigured values using the event hook:
Add new widgets
The following code shows how to add thetoggle-refinement
widget to the InstantSearch page:
Templates
Follow best practices when you update templates in the extension. Keep changes in your theme directory and avoid directly editing the extension.InstantSearch page wrapper
The wrapper template holds the layout of the InstantSearch results page, along with all other templates rendered in to it. To alter the layout of this page, go to thetemplates
directory,
and locate the wrapper.phtml
file.
This file is a standard Magento template file.
InstantSearch results page
To change the structure of the widgets on the results page, go to theinstant
folder of the templates
directory.
Here are the files used to render the configured widgets:
hit.phtml
- The template for a single productfacet.phtml
- The template for a single filter/facetrefinements.phtml
- The template for current refinementsstats.phtml
- The template for search statistics
Mustache templates
In the Magento extension, the Autocomplete and Recommend templates use the JavaScript-based approach. The InstantSearch templates still rely on Hogan.js which is still supported in InstantSearch.js v4. A Mustache-based template parser, such as Hogan.js, must be loaded explicitly in these files:wrapper.phtml
stats.phtml
algoliaBundle
.
Since the algoliaBundle
is discontinued in version 3.15.0,
Hogan.js is now packaged as a separate library that can be loaded through RequireJS as needed.
However, Hogan.js is unmaintained and security concerns persist. That’s why Mustache.js is the default templating engine.
Template engines
You don’t have to use Mustache.js if it doesn’t suit your purpose. You can use RequireJS mixins to specify an alternate template engine. For example, to use Hogan.js instead of Mustache.js, you can write a mixin on thegetSelectedEngineType
function:
-
requirejs-config.js
js -
template-engine-mixin.js
JavaScript
CustomAlgolia
sample module.