Custom hook events
Custom frontend hooks let you modify the autocomplete menu and search results page in the Algolia app for Shopify.
Hooks are functions that run at specific points of the Algolia app’s lifecycle. They let you register custom functions that change certain aspects of the Autocomplete and InstantSearch integrations. To listen to global events without passing or changing data, see Global events.
To view the available hooks, go to your Shopify store and enter window.algoliaShopify.hooks.allowedHooks
into the developer tools’ console of your browser.
Add custom hooks
To use these hooks, add a new JavaScript file to your theme.
Create the file
-
Go to your theme list, select the theme you want to customize, and click the three dots next to the theme to reveal the menu.
-
Select Edit code.
-
From the Assets section in the left-hand menu, click Add a new asset.
-
Select the Create a blank file tab and then select js from the drop-down menu.
-
Enter the name of the file you want to create, for example,
algolia_custom_hooks
. -
Click Done.
Include the file in your theme
The next step is to include this new JavaScript file in the theme:
-
In the Edit code tab, open the
theme.liquid
file. -
Scroll to the bottom of the
theme.liquid
file and add the following just before the</body>
tag. ReplaceNEW_FILE
with the filename of the JavaScript file with your custom hook:html -
Save the file.
Use custom hooks
To register a function to a hook,
open the specific hooks JavaScript file,
add an event listener to the algolia.hooks.initialize
event,
and call the algoliaShopify.hooks.registerHook
function.
For example:
The return values of the function you register with the hook must match the expected return type, indicated by the hook’s name.
Hook name ends with | Expected return type |
---|---|
Options | Object |
Template | tagged template literal |
Array | Array of strings or objects |
String | String |
Number | Number |
Action | Function |
Autocomplete hooks
The following hooks change the behavior of the autocomplete menu:
beforeAutocompleteOptions
beforeAutocompleteFiltersString
beforeAutocompleteRedirectUrlOptions
beforeAutocompleteMainTemplate
beforeAutocompleteMainProductsTemplate
beforeAutocompleteNoResultsTemplate
beforeAutocompleteHeaderTemplate
beforeAutocompleteFooterTemplate
beforeAutocompleteProductTemplate
beforeAutocompleteArticlesTemplate
beforeAutocompleteCollectionsTemplate
beforeAutocompletePagesTemplate
beforeAutocompleteSuggestionsTemplate
beforeAutocompleteProductTransformResponseHits
afterAutocompleteProductClickAction
beforeAutocompleteOptions
Sets parameters for the autocomplete menu.
Example:
beforeAutocompleteFiltersString
Adds filter parameter to the autocomplete menu.
Example:
beforeAutocompleteRedirectUrlOptions
Changes the default parameters
of the createRedirectUrlPlugin
function.
Example:
beforeAutocompleteMainTemplate
Changes the HTML template that renders the Autocomplete panel.
You can use this to render an Autocomplete multi-panel layout.
The elements
object has the following properties:
querySuggestionsPlugin
collections
articles
pages
redirectUrlPlugin
products
Example:
beforeAutocompleteMainProductsTemplate
Renders the product section in the autocomplete menu.
Example:
beforeAutocompleteNoResultsTemplate
Renders when there are no search results.
Example:
beforeAutocompleteHeaderTemplate
Renders a header section at the top of the autocomplete results panel.
Example:
beforeAutocompleteFooterTemplate
Renders a footer section at the bottom of the autocomplete results panel. To render a footer, select the Show See All products option in the Autocomplete Search options in your store’s admin.
Example:
beforeAutocompleteProductTemplate
Template for rendering each product hit in the autocomplete results.
Example:
beforeAutocompleteArticlesTemplate
Template for rendering each article hit in the autocomplete results.
Example:
beforeAutocompleteCollectionsTemplate
Template for rendering each collection hit in the autocomplete results.
Example:
beforeAutocompletePagesTemplate
Template for rendering each pages hit in the autocomplete results.
Example:
beforeAutocompleteSuggestionsTemplate
Template for rendering each search suggestion in the autocomplete menu.
Example:
beforeAutocompleteProductTransformResponseHits
Modifies the hits
before rendering them in the autocomplete menu.
For more information, see transformresponse
.
Example:
afterAutocompleteProductClickAction
Function that should run after a product is clicked.
Example:
InstantSearch hooks
The following hooks change the behavior of the search results page:
beforeInstantSearchConfigurationOptions
beforeInstantSearchOptions
beforeInstantSearchAllowParamsArray
beforeInstantSearchFiltersString
beforeInstantSearchMainTemplate
beforeInstantSearchProductTemplate
beforeInstantSearchNoResultTemplate
beforeInstantSearchFacetItemTemplate
beforeInstantSearchShowMoreTemplate
beforeInstantSearchStatsTemplate
beforeISTransformItems
beforeISStartAddWidgetArray
afterISStartRemoveDefaultWidget
afterInstantSearchHitClickAction
beforeInstantSearchFacetLimitNumber
beforeISFacetSearchablePlaceholderString
beforeISFacetSearchableNoResultsString
beforeInstantSearchFacetHeaderString
beforeInstantSearchFacetTransformItemsOptions
beforeISearchInitSortOrdersArray
beforeISInitCollectionSortOrdersArray
beforeInstantSearchConfigurationOptions
Changes InstantSearch options.
Example:
beforeInstantSearchOptions
Changes the following parameters:
colors
distinct
facets
hitsPerPage
selector
sortOrders
beforeInstantSearchAllowParamsArray
Preserves URL parameters when navigating through the search results.
Example:
beforeInstantSearchFiltersString
Changes the filter parameter of the search results page.
Example:
beforeInstantSearchMainTemplate
Template for the main template container.
Example:
beforeInstantSearchProductTemplate
Template for product hits in the search results.
Example:
beforeInstantSearchNoResultTemplate
Template for when there are no results.
Example:
beforeInstantSearchFacetItemTemplate
Template for rendering facet items.
Example:
beforeInstantSearchShowMoreTemplate
Template for the Show more button
Example:
beforeInstantSearchStatsTemplate
Template for search stats
Example:
beforeISTransformItems
Change items before they’re rendered.
beforeISStartAddWidgetArray
Add InstantSearch widgets to the search results page.
Not all InstantSearch.js widgets are available.
Example:
afterISStartRemoveDefaultWidget
Remove default widgets from the search results page.
You can remove these widgets (widget.widgetType
):
ais.sortBy
ais.searchBox
ais.stats
ais.hits
ais.pagination
Example:
afterInstantSearchHitClickAction
Run a function after a search result is clicked.
Example:
beforeInstantSearchFacetLimitNumber
Changes the default number of facet values (default: 10).
Example:
beforeISFacetSearchablePlaceholderString
Changes the placeholder string for a searchable facet.
beforeISFacetSearchableNoResultsString
Changes the no-results string for searchable facets. You can return a template literal or a string.
beforeInstantSearchFacetHeaderString
Changes the facet header string.
beforeInstantSearchFacetTransformItemsOptions
Transforms facet values before they’re rendered.
beforeISearchInitSortOrdersArray
Transform sort order before they’re rendered.
beforeISInitCollectionSortOrdersArray
Transform the sort order for collections.
Recommend hooks
The following hooks can be used to change the behavior of recommendations from Algolia Recommend.
beforeRecommendHitTemplate
Can be used to modify the HTML template that renders the panel with recommendations.
Insights hooks
The following hooks change aspects of Insights integration for sending click and conversion events.
beforeAlgoliaAnalyticsOptions
Changes parameters of the Insights library.
Example:
beforeAddToCartSelectorString
Changes the CSS selector for the add-to-cart button.
Example:
Was this page helpful?