frequentlyBoughtTogether
frequentlyBoughtTogether({ container: string|HTMLElement, objectIDs: string[], // Optional parameters limit: number, threshold: number, queryParameters: object, escapeHTML: boolean, templates: object, cssClasses: object, transformItems: function, });
1
import { frequentlyBoughtTogether } from 'instantsearch.js/es/widgets';
About this widget
Use the frequentlyBoughtTogether
widget to display a list of frequently bought together items.
If you’re using the deprecated recommend-js
UI library, please refer to the API reference for the frequentlyBoughtTogether
function.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
frequentlyBoughtTogether({
container: '#frequentlyBoughtTogether',
objectIDs: ['5723537'],
templates: {
item(recommendation, { html }) {
return html`
<h2>${recommendation.name}</h2>
<p>${recommendation.description}</p>
`;
},
},
});
With a carousel layout
Display the frequentlyBoughtTogether
widget as a scrollable carousel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { carousel } from 'instantsearch.js/es/templates';
// or
const { carousel } = instantsearch.templates;
frequentlyBoughtTogether({
container: '#frequentlyBoughtTogether',
objectIDs: ['5723537'],
templates: {
item(recommendation, { html }) {
return html`
<h2>${recommendation.name}</h2>
<p>${recommendation.description}</p>
`;
},
layout: carousel(),
},
});
Options
container
|
type: string|HTMLElement
Required
The CSS Selector or |
||
Copy
|
|||
objectIDs
|
type: string[]
Required
The list of Each |
||
Copy
|
|||
limit
|
type: number
The number of recommendations to retrieve. Depending on the available recommendations and the other request parameters, the actual number of items may be lower than that.
If |
||
Copy
|
|||
threshold
|
type: number
The threshold for the recommendations confidence score (between 0 and 100). Only recommendations with a greater score are returned. |
||
Copy
|
|||
queryParameters
|
type: Omit<SearchParameters, 'page' | 'hitsPerPage' | 'offset' | 'length'>
List of search parameters to send. |
||
Copy
|
|||
escapeHTML
|
type: boolean
default: true
Optional
Escapes HTML entities from recommendations string values. |
||
Copy
|
|||
templates
|
type: object
Optional
The templates to use for the widget. |
||
Copy
|
|||
cssClasses
|
type: object
default: {}
Optional
The CSS classes you can override:
|
||
Copy
|
|||
transformItems
|
type: function
default: items => items
Optional
Receives the recommendations and is called before displaying them. It returns a new array with the same “shape” as the original. This is helpful when transforming or reordering items. The entire |
||
Copy
|
Templates
You can customize parts of the widget’s UI using the Templates API.
Every template provides an html
function you can use as a tagged template. Using html
lets you safely provide templates as an HTML string. It works directly in the browser without a build step. See Templating your UI for more information.
The html
function is available starting from v4.46.0.
empty
|
type: string|function
Optional
The template to use when there are no recommendations. |
||
Copy
|
|||
header
|
type: string|function
Optional
The template to use for the recommendations header. This template receives the recommendations as well as the |
||
Copy
|
|||
item
|
type: string|function
Optional
The template to use for each recommendation. This template receives an object containing a single record. |
||
Copy
|
|||
layout
|
since: v4.74.0
type: function
Optional
The template to use to wrap all items. InstantSearch.js provides an out-of-the-box carousel layout template with the following options:
|
||
Copy
|
HTML output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<section class="ais-FrequentlyBoughtTogether">
<h3 class="ais-FrequentlyBoughtTogether-title">Frequently Bought Together</h3>
<div class="ais-FrequentlyBoughtTogether-container">
<ol class="ais-FrequentlyBoughtTogether-list">
<li class="ais-FrequentlyBoughtTogether-item">
...
</li>
<li class="ais-FrequentlyBoughtTogether-item">
...
</li>
<li class="ais-FrequentlyBoughtTogether-item">
...
</li>
</ol>
</div>
</section>
Customize the UI with connectFrequentlyBoughtTogether
If you want to create your own UI of the frequentlyBoughtTogether
widget, you can use connectors.
To use connectFrequentlyBoughtTogether
, you can import it with the declaration relevant to how you installed InstantSearch.js.
1
import { connectFrequentlyBoughtTogether } from 'instantsearch.js/es/connectors';
Then it’s a 3-step process:
// 1. Create a render function
const renderFrequentlyBoughtTogether = (renderOptions, isFirstRender) => {
// Rendering logic
};
// 2. Create the custom widget
const customFrequentlyBoughtTogether = connectFrequentlyBoughtTogether(
renderFrequentlyBoughtTogether
);
// 3. Instantiate
search.addWidgets([
customFrequentlyBoughtTogether({
// instance params
})
]);
Create a render function
This rendering function is called before the first search (init
lifecycle step)
and each time results come back from Algolia (render
lifecycle step).
const renderFrequentlyBoughtTogether = (renderOptions, isFirstRender) => {
const {
object[] items,
object widgetParams,
} = renderOptions;
if (isFirstRender) {
// Do some initial rendering and bind events
}
// Render the widget
}
Rendering options
items
|
type: object[]
The matched recommendations from the Algolia API. |
||
Copy
|
|||
widgetParams
|
type: object
All original widget options forwarded to the render function. |
||
Copy
|
Create and instantiate the custom widget
We first create custom widgets from our rendering function, then we instantiate them. When doing that, there are two types of parameters you can give:
- Instance parameters: they are predefined parameters that you can use to configure the behavior of Algolia.
- Your own parameters: to make the custom widget generic.
Both instance and custom parameters are available in connector.widgetParams
, inside the renderFunction
.
const customFrequentlyBoughtTogether = connectFrequentlyBoughtTogether(
renderFrequentlyBoughtTogether
);
search.addWidgets([
customFrequentlyBoughtTogether({
objectIDs: string[],
// Optional parameters
limit: number,
threshold: number,
queryParameters: object,
escapeHTML: boolean,
transformItems: function,
})
]);
Instance options
objectIDs
|
type: string[]
Required
The list of Each |
||
Copy
|
|||
limit
|
type: number
The number of recommendations to retrieve. Depending on the available recommendations and the other request parameters, the actual number of items may be lower than that.
If |
||
Copy
|
|||
threshold
|
type: number
The threshold for the recommendations confidence score (between 0 and 100). Only recommendations with a greater score are returned. |
||
Copy
|
|||
queryParameters
|
type: Omit<SearchParameters, 'page' | 'hitsPerPage' | 'offset' | 'length'>
List of search parameters to send. |
||
Copy
|
|||
escapeHTML
|
type: boolean
default: true
Optional
Escapes HTML entities from recommendations string values. |
||
Copy
|
|||
transformItems
|
type: function
default: items => items
Optional
Receives the recommendations and is called before displaying them. It returns a new array with the same “shape” as the original. This is helpful when transforming or reordering items. The entire |
||
Copy
|
Full example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Create the render function
const renderFrequentlyBoughtTogether = (renderOptions, isFirstRender) => {
const { items, widgetParams } = renderOptions;
widgetParams.container.innerHTML = `
<ul>
${items
.map(item => `<li>${item.name}</li>`)
.join('')}
</ul>
`;
};
// Create the custom widget
const customFrequentlyBoughtTogether = connectFrequentlyBoughtTogether(
renderFrequentlyBoughtTogether
);
// Instantiate the custom widget
search.addWidgets([
customFrequentlyBoughtTogether({
container: document.querySelector('#frequentlyBoughtTogether'),
objectIDs: ['5723537'],
})
]);