beforeAutocompleteAsyncFunction
Use this hook to run asynchronous work (for example, external API requests) before Autocomplete continues.
Parameters
This hook doesn’t accept parameters.
Returns
This hook doesn’t return a value. Return a Promise to delay Autocomplete until your work completes.
Examples
Delay Autocomplete until an async task completes
const sleep = ( ms , hookName ) =>
new Promise (( resolve ) => {
console . log ( "sleeping for " , ms , hookName );
setTimeout ( resolve , ms );
});
document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteAsyncFunction" ,
async () => {
console . log (
"----------- beforeAutocompleteAsyncFunction started ----------------" ,
);
await sleep ( 1000 , "beforeAutocompleteAsyncFunction" );
},
);
});
beforeAutocompleteConfigurationOptions
Modifies the global Autocomplete configuration before any plugin executes a .
This hook runs on every keystroke and lets you customize search parameters,
apply a , update analytics tags, or change other search query settings across all Autocomplete sources.
Parameters
Configuration object. Instance of an Algolia Search API client.
Context information. Name of the plugin triggering the query, such as products, collections, articles, or pages.
Unique identifier for the Autocomplete source .
Returns
Modified configuration object.
Examples
Add analytics tags to product queries
Add dynamic filters based on query
This example adds the filter tags:sale to the search,
if the query includes the word sale. document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteConfigurationOptions" ,
( config , context ) => {
if (
context . pluginName === "products" &&
config . queries [ 0 ]. query . includes ( "sale" )
) {
config . queries = config . queries . map (( query ) => ({
... query ,
params: {
... query . params ,
filters: "tags:sale" ,
},
}));
}
return config ;
},
);
});
Add rule contexts to product queries
This example adds custom ruleContexts to the product queries in Autocomplete. document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteConfigurationOptions" , ( config , context ) => {
if ( context . sourceId === "products" ) {
config . queries = config . queries . map (( query ) => ({
... query ,
params: {
... query . params ,
ruleContexts: [ "custom-context" , "test-context" ],
},
}));
}
return config ;
},
);
});
beforeAutocompletePluginOptions
Customizes individual plugin options during plugin construction.
This hook runs once per plugin at initialization time.
Use the hook to set static defaults like base filters, analytics tags, or custom transform functions.
Parameters
Plugin options. Unique identifier for the Autocomplete source.
Function for transforming items before they’re rendered.
Context information. Name of the plugin, such as products, collections, articles, or pages.
Unique identifier for the Autocomplete source .
Returns
Examples
Set base filters per plugin
document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompletePluginOptions" ,
( options , context ) => {
if ( context . pluginName === "products" ) {
options . searchParameters . filters = "inventory_quantity > 0" ;
} else if ( context . pluginName === "collections" ) {
options . searchParameters . filters = "published:true" ;
}
return options ;
},
);
});
Customize hits per page per plugin
document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompletePluginOptions" ,
( options , context ) => {
if ( context . pluginName === "products" ) {
options . searchParameters . hitsPerPage = 12 ;
} else if ( context . pluginName === "articles" ) {
options . searchParameters . hitsPerPage = 5 ;
}
return options ;
},
);
});
beforeAutocompletePluginsArray
Manages the plugin array before creating the Autocomplete instance.
This hook runs once at initialization and lets you add and remove plugins, or reorder the plugin array.
To work with plugins, use the arguments object to extract them into an array.
Parameters
List of plugins passed to the Autocomplete instance.
Returns
Examples
Remove articles and pages plugins
This example removes the articles and pages plugins. document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompletePluginsArray" ,
function () {
// Extract plugins from arguments
var plugins = Array . prototype . slice . call ( arguments );
// Remove articles and pages plugins
return plugins . filter (( plugin ) => {
if ( ! plugin || ! plugin . getSources ) return true ;
var sources = plugin . getSources ({ query: "" });
if ( ! sources || ! sources . length ) return true ;
// Remove plugins that expose articles or pages
return ! sources . some (
( source ) =>
source . sourceId === "articles" || source . sourceId === "pages" ,
);
});
},
);
});
beforeAutocompleteOptions
Use this hook to set or change Autocomplete options, such as the placeholder text.
Parameters
Returns
Modified Autocomplete options.
Examples
Change the placeholder text
This example changes the placeholder text. document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook ( "beforeAutocompleteOptions" , ( options ) => {
// Change the placeholder text of the Autocomplete menu
options . placeholder = "Search our products" ;
return options ;
});
});
Use this hook to add or override filters in the Autocomplete menu.
Parameters
Returns
Examples
This example returns a custom filter instead of the default. document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteFiltersString" ,
( _defaultFilter ) => {
// Return a custom filter instead of the default filter.
return "price > 17" ;
},
);
});
beforeAutocompleteRedirectUrlOptions
Changes the default parameters of the
createRedirectUrlPlugin function.
Parameters
Redirect URL plugin options.
Returns
Modified Redirect URL plugin options.
Examples
Add a custom class to redirect links
This example changes the template for the redirect link by adding a custom CSS class. document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteRedirectUrlOptions" ,
( options ) => {
// Change the default template for rendering redirect items
return {
templates: {
item ({ html , state }) {
return html `<a className="myCustomClass"> ${ state . query } </a>` ;
},
},
};
},
);
});
beforeAutocompleteMainTemplate
Changes the HTML template that renders the Autocomplete panel .
Use this to render an Autocomplete multi-panel layout.
Parameters
Template options including the html tagged template function.
Template elements. Query suggestions panel element.
Collections panel element.
Redirect URL panel element.
Whether to render query suggestions.
Returns
Examples
Render a two-column layout
This example returns a custom two-column layout as template. document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteMainTemplate" ,
( _defaultTemplate , templateOptions , elements , displaySuggestions ) => {
const { html } = templateOptions ;
// Don't return the default template and return a custom two-column layout instead
return html `
<div class="aa-PanelLayout aa-Panel--scrollable">
<div class="aa-PanelSections">
<div class="aa-PanelSection--left">
${
displaySuggestions &&
html `
<div class="aa-SourceHeader">
<span class="aa-SourceHeaderTitle"
> ${ algoliaShopify . translations . suggestions } </span
>
<div class="aa-SourceHeaderLine" />
</div>
${ elements . querySuggestionsPlugin }
`
}
${ elements . collections } ${ elements . articles } ${ elements . pages }
${ elements . redirectUrlPlugin }
</div>
<div class="aa-PanelSection--right">
${ elements . products }
</div>
</div>
</div>
` ;
},
);
});
beforeAutocompleteMainProductsTemplate
Renders the product section in the Autocomplete menu.
Parameters
Template options including the html tagged template function.
Contains the products element. Products section element.
Returns
Examples
Render the products section
document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteMainProductsTemplate" ,
( _defaultTemplate , templateOptions , elements ) => {
const { html } = templateOptions ;
return html `
<div class="aa-PanelLayout aa-Panel--scrollable">
<div class="aa-PanelSection">
${ elements . products }
</div>
</div>
` ;
},
);
});
beforeAutocompleteNoResultsTemplate
Renders a template when Autocomplete doesn’t return results.
Parameters
Template options including the html and state properties.
Returns
Examples
Customize the no results panel
document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteNoResultsTemplate" ,
( _defaultTemplate , templateOptions ) => {
const { html , state } = templateOptions ;
return html `
<div class="aa-PanelLayout aa-Panel--scrollable">
<p class="aa-NoResultsHeader">
${ algoliaShopify . translation_helpers . no_result_for ( state . query ) }
</p>
<a class="aa-NoResultsLink" href=" ${ window . Shopify . routes . root } search?q=">
${ algoliaShopify . translations . allProducts }
</a>
</div>
` ;
},
);
});
Renders a header section at the top of the Autocomplete results panel.
Parameters
Template options including the html and state properties.
Returns
Examples
Customize the results header
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.
Parameters
Template options including the html and state properties.
Returns
Examples
Render a footer with a “See all products” link
beforeAutocompleteProductTemplate
Template for rendering each product hit in the Autocomplete results.
If you are using this template, ensure you also call trackSearchAttribution(item) to properly handle events.
Parameters
Template options including html, item, and components.
Whether distinct mode is enabled.
Function to track search attribution.
Returns
Examples
document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteProductTemplate" ,
(
_defaultTemplate ,
templateOptions ,
distinct ,
itemLink ,
trackSearchAttribution ,
) => {
// Modify default options, then return them
const { html , item , components } = templateOptions ;
return html `
<a
href=" ${ itemLink } "
class="aa-ItemLink aa-ProductItem"
onClick=" ${ () => trackSearchAttribution ( item ) } "
>
<div class="aa-ItemContent">
<div class="aa-ItemPicture aa-ItemPicture--loaded">
<img
src=" ${ algoliaShopify . helpers . renderImage ( item , 125 ). src } "
srcset=" ${ algoliaShopify . helpers . renderImage ( item , 125 ). srcset } "
sizes=" ${ algoliaShopify . helpers . renderImage ( item , 125 ). sizes } "
alt=" ${ item . title } "
/>
</div>
<div class="aa-ItemContentBody">
<div class="aa-ItemContentBrand">
${
item . product_type &&
components . Highlight ({ hit: item , attribute: "product_type" })
}
removing vendor
</div>
<div class="aa-ItemContentTitleWrapper">
<div class="aa-ItemContentTitle">
${ components . Highlight ({ hit: item , attribute: "title" }) }
<span class="algolia-variant">
${ algoliaShopify . helpers . variantTitleAddition ( item , distinct ) }
</span>
</div>
</div>
<div class="aa-ItemContentPrice">
<div class="aa-ItemContentPriceCurrent">
${ algoliaShopify . helpers . displayPrice ( item , distinct ) }
</div>
</div>
</div>
</div>
</a>
` ;
},
);
});
beforeAutocompleteArticlesTemplate
Template for rendering each article hit in the Autocomplete results.
Parameters
Template options including html, item, and components.
Returns
Examples
document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteArticlesTemplate" ,
( _defaultTemplate , templateOptions , itemLink ) => {
const { item , html , components } = templateOptions ;
return html `
<a href=" ${ itemLink } " class="aa-ItemLink">
<div class="aa-ItemWrapper">
<div class="aa-ItemContent">
<div class="aa-ItemIcon aa-ItemIcon--noBorder">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"
/>
</svg>
</div>
<div class="aa-ItemContentBody">
<div class="aa-ItemContentTitle">
${ components . Highlight ({ hit: item , attribute: "title" }) }
</div>
</div>
</div>
</div>
</a>
` ;
},
);
});
beforeAutocompleteCollectionsTemplate
Template for rendering each collection hit in the Autocomplete results.
Parameters
Template options including html, item, and components.
Returns
Examples
Customize collection items
document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteCollectionsTemplate" ,
( _defaultTemplate , templateOptions , itemLink ) => {
const { html , item , components } = templateOptions ;
return html `
<a href=" ${ itemLink } " class="aa-ItemLink">
<div class="aa-ItemWrapper">
<div class="aa-ItemContent">
<div class="aa-ItemIcon aa-ItemIcon--noBorder">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zM14 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zM14 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z"
/>
</svg>
</div>
<div class="aa-ItemContentBody">
<div class="aa-ItemContentTitle">
${ components . Highlight ({ hit: item , attribute: "title" }) }
</div>
</div>
</div>
</div>
</a>
` ;
},
);
});
beforeAutocompletePagesTemplate
Template for rendering each pages hit in the Autocomplete results.
Parameters
Template options including html, item, and components.
Returns
Examples
document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompletePagesTemplate" ,
( _defaultTemplate , templateOptions , itemLink ) => {
const { html , item , components } = templateOptions ;
return html `
<a href=" ${ itemLink } " class="aa-ItemLink aa-ProductItem">
<div class="aa-ItemWrapper">
<div class="aa-ItemContent">
<div class="aa-ItemIcon aa-ItemIcon--noBorder">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
</div>
<div class="aa-ItemContentBody">
<div class="aa-ItemContentTitle">
${ components . Highlight ({ hit: item , attribute: "title" }) }
</div>
</div>
</div>
</div>
</a>
` ;
},
);
});
beforeAutocompleteSuggestionsTemplate
Template for rendering each search suggestion in the Autocomplete menu.
Parameters
Template options including html, item, and components.
Returns
Examples
document . addEventListener ( "algolia.hooks.initialize" , () => {
algoliaShopify . hooks . registerHook (
"beforeAutocompleteSuggestionsTemplate" ,
( _defaultTemplate , templateOptions ) => {
const { html , item , components } = templateOptions ;
return html `
<a
class="aa-ItemLink aa-ItemWrapper"
href=" ${ window . Shopify . routes . root } search?q= ${ item . query } "
>
<div class="aa-ItemContent">
<div class="aa-ItemIcon aa-ItemIcon--noBorder">
<svg viewBox="0 0 24 24" fill="currentColor">
<path d="M16.041 15.856c-0.034 0.026-0.067 0.055-0.099 0.087s-0.060 0.064-0.087 0.099c-1.258 1.213-2.969 1.958-4.855 1.958-1.933 0-3.682-0.782-4.95-2.050s-2.050-3.017-2.050-4.95 0.782-3.682 2.050-4.95 3.017-2.050 4.95-2.050 3.682 0.782 4.95 2.050 2.050 3.017 2.050 4.95c0 1.886-0.745 3.597-1.959 4.856zM21.707 20.293l-3.675-3.675c1.231-1.54 1.968-3.493 1.968-5.618 0-2.485-1.008-4.736-2.636-6.364s-3.879-2.636-6.364-2.636-4.736 1.008-6.364 2.636-2.636 3.879-2.636 6.364 1.008 4.736 2.636 6.364 3.879 2.636 6.364 2.636c2.125 0 4.078-0.737 5.618-1.968l3.675 3.675c0.391 0.391 1.024 0.391 1.414 0s0.391-1.024 0-1.414z" />
</svg>
</div>
<div class="aa-ItemContentBody">
<div class="aa-ItemContentTitle">
${ components . Highlight ({ hit: item , attribute: "query" }) }
</div>
</div>
</div>
<div class="aa-ItemActions">
<button
class="aa-ItemActionButton"
title="Fill query with ${ item . query } "
>
<svg viewBox="0 0 24 24" fill="currentColor">
<path d="M8 17v-7.586l8.293 8.293c0.391 0.391 1.024 0.391 1.414 0s0.391-1.024 0-1.414l-8.293-8.293h7.586c0.552 0 1-0.448 1-1s-0.448-1-1-1h-10c-0.552 0-1 0.448-1 1v10c0 0.552 0.448 1 1 1s1-0.448 1-1z" />
</svg>
</button>
</div>
</a>
` ;
},
);
});
Modifies the hits before rendering them in the Autocomplete menu.
For more information, see transformResponse .
Parameters
Full results from Algolia.
Returns
Examples
Transform product hits before rendering
afterAutocompleteProductClickAction (deprecated)
Adds a click handler function to the product template.
Call this hook after a user clicks a product.
Parameters
First parameter (unused).
Product line item that was clicked.
See also