Snippet configuration reference
The snippet configuration defines how the Motive's search layer works in a shop that doesn't use a Motive plugin. You add it to window.motive, before the Motive script.
interact
To learn more on how to add the snippet configuration, see Install Motive on other platforms.
Snippet parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
xEngineId | string | — | Required. Unique ID of your search engine. Example: 00000000-0000-0000-0000-000000000000 |
lang | string | 'en' | Language of the search layer. Examples: 'es', 'en-GB' |
currency | string | 'EUR' | ISO 4217 (opens new window) code of the currency for prices and the price filter. Examples: 'USD', 'GBP' |
triggerSelector | string | '#search_widget' | CSS selector of the element that opens the search layer. Examples: '.search-input', '#search_widget form' |
isolated | boolean | — | If true, the search layer runs in a Shadow DOM. Your shop's styles don't affect it, and your shop's previous search box doesn't open. |
cartUrl | string | — | URL of your shop's cart page. If you set it, the search layer shows a cart button that links to this page. Otherwise, the cart button is hidden. Examples: '/cart', 'https://www.example.com/cart' |
externalOpen | boolean | false | If true, the search layer doesn't open when shoppers select the trigger element. |
callbacks | object | — | Object that contains the callback functions that connect the search layer to your shop. See Callbacks. |
transformPriceRange | function | — | Changes the values of the price range filter. See Changing prices. |
Callbacks
Callbacks are optional. Add each callback as a property of the callbacks object, inside window.motive:
window.motive = () => ({
// ...your snippet configuration
callbacks: {
UserClickedResultAddToCart: addToCart,
CartHandlerGettingCartInfo: getCartInfo,
},
})
| Callback | Receives | Returns | Description |
|---|---|---|---|
UserClickedResultAddToCart | Product | Promise<void> | Adds the product to your shop's cart. The promise resolves once the product is added. Example: result => new Promise(resolve => { console.log(result); resolve(); }) |
CartHandlerGettingCartInfo | — | { productsCount: number \| string } | Returns the number of products in the cart. The object can include other keys. Example: () => ({ productsCount: 5 }) |
AppendedResultsChanged | Array of results | Array of partial results, or a promise of one | Changes result data, such as prices, when results load. Example: results => results.map(({ id, price }) => ({id, price: { value: price.value * 0.8 }})) |
RecommendationsChanged | Array of recommendations | Array of partial results, or a promise of one | Changes recommendation data, such as prices, when recommendations load. Example: recommendations => recommendations.map(({ id, price }) => ({id, price: { value: price.value * 0.8 }})) |
QueriesPreviewChanged | Object of search previews | Object of search previews, or a promise of one | Changes the results shown in search previews, such as prices. Example: queriesPreview => Object.fromEntries(Object.entries(queriesPreview).map(([queryHash, queryPreview]) => [queryHash, { ...queryPreview, results: queryPreview.results.map(({ id, price }) => ({ id, price: { value: price.value * 0.8 } })) }])) |
Each partial result includes the product id and the fields to change.
Example:
{ id: '12345', price: { value: 19.99 } }
Changing prices
Use callbacks to show prices that depend on the shopper, such as discounts for logged-in customers. To change prices consistently across the search layer, use these together:
AppendedResultsChanged,RecommendationsChangedandQueriesPreviewChanged: Change product prices in results, recommendations and search previews.transformPriceRange: Changes the values of the price range filter, so it matches the changed prices.transformPriceRangereceives the minimum and maximum prices of the filter and returns an array with the new values.
Example:
transformPriceRange: (min, max) => [min * 0.8, max * 0.8]
Updating the configuration
To change the configuration without reloading the page, for example when a shopper changes language or currency, use InterfaceX.setSnippetConfig. It accepts any of the parameters on this page and updates the search layer.
Example:
InterfaceX.setSnippetConfig({
currency: 'USD',
})
If your shop reloads the page when the language or currency changes, you don't need InterfaceX.setSnippetConfig. The search layer starts again and reads the new values from window.motive.
Full example
This example connects the cart and applies a 20 % discount to all prices in the search layer, including the price range filter. Define the callback functions before window.motive.
<script>
// Adds the product to your shop's cart. Must return a promise.
function addToCart(result) {
return myShop.addToCart(result.id)
}
// Returns the number of products in the cart.
function getCartInfo() {
return { productsCount: myShop.getCartCount() }
}
// Applies the discount to results and recommendations.
function applyDiscount(results) {
return results.map(({ id, price }) => ({
id,
price: { value: price.value * 0.8 },
}))
}
// Applies the discount to the results in search previews.
function applyDiscountToPreviews(queriesPreview) {
return Object.fromEntries(
Object.entries(queriesPreview).map(([key, preview]) => [
key,
{
...preview,
results: preview.results.map(result => ({
...result,
price: { ...result.price, value: result.price.value * 0.8 },
})),
},
]),
)
}
// Applies the discount to the price range filter.
function applyDiscountToPriceRange(min, max) {
return [min * 0.8, max * 0.8]
}
window.motive = {
xEngineId: 'YOUR_ENGINE_ID',
lang: 'en',
currency: 'EUR',
triggerSelector: '#search-input',
isolated: true,
cartUrl: '/cart',
callbacks: {
UserClickedResultAddToCart: addToCart,
CartHandlerGettingCartInfo: getCartInfo,
AppendedResultsChanged: applyDiscount,
RecommendationsChanged: applyDiscount,
QueriesPreviewChanged: applyDiscountToPreviews,
},
transformPriceRange: applyDiscountToPriceRange,
}
</script>
<script type="module" src="https://assets.motive.co/front-loader/custom/v1.js"></script>
In this example, myShop.addToCart and myShop.getCartCount represent your shop's own functions. Replace them with the functions your shop uses to manage the cart.