frequentlyBoughtTogether
The frequentlyBoughtTogether
function lets you render Frequently Bought Together items.
Installation
The Recommend JavaScript package is available on the npm registry.
1
2
3
yarn add @algolia/recommend-js
# or
npm install @algolia/recommend-js
If you don’t want to use a package manager, you can use a standalone endpoint:
1
2
3
4
5
6
<script src="https://cdn.jsdelivr.net/npm/@algolia/recommend-js"></script>
<script src="https://cdn.jsdelivr.net/npm/@algolia/recommend"></script>
<script>
const { frequentlyBoughtTogether, relatedProducts, trendingItems, trendingFacets } = window['@algolia/recommend-js'];
const recommend = window['@algolia/recommend'];
</script>
Usage
To get started, you need to specify a container to inject the component into.
1
<div id="frequentlyBoughtTogether"></div>
Then, you can call the frequentlyBoughtTogether
function and provide the container. It can be a CSS selector or an Element.
You can customize how to render each item by passing a custom component to the itemComponent
prop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/** @jsx h */
import { h } from 'preact';
import { frequentlyBoughtTogether } from '@algolia/recommend-js';
import recommend from '@algolia/recommend';
const recommendClient = recommend('YourApplicationID', 'YourSearchOnlyAPIKey');
const indexName = 'YOUR_INDEX_NAME';
const currentObjectID = 'YOUR_OBJECT_ID';
frequentlyBoughtTogether({
container: '#frequentlyBoughtTogether',
recommendClient,
indexName,
objectIDs: [currentObjectID],
itemComponent({ item }) {
return (
<pre>
<code>{JSON.stringify(item)}</code>
</pre>
);
},
});
If you don’t use JSX in your project, you can either use createElement
from Preact directly, or return tagged templates using htm
.
With createElement
You can import createElement
(and Fragment
if needed) from Preact to create components without JSX.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { createElement } from 'preact';
import { frequentlyBoughtTogether } from '@algolia/recommend-js';
import recommend from '@algolia/recommend';
const recommendClient = recommend('YourApplicationID', 'YourSearchOnlyAPIKey');
const indexName = 'YOUR_INDEX_NAME';
const currentObjectID = 'YOUR_OBJECT_ID';
frequentlyBoughtTogether({
container: '#frequentlyBoughtTogether',
recommendClient,
indexName,
objectIDs: [currentObjectID],
itemComponent({ item }) {
return createElement(
'pre',
null,
createElement('code', null, JSON.stringify(item))
);
},
});
With htm
If you don’t want to use JSX or createElement
, you can use the
htm
library to write components with tagged templates.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { html } from 'htm/preact';
import { frequentlyBoughtTogether } from '@algolia/recommend-js';
import recommend from '@algolia/recommend';
const recommendClient = recommend('YourApplicationID', 'YourSearchOnlyAPIKey');
const indexName = 'YOUR_INDEX_NAME';
const currentObjectID = 'YOUR_OBJECT_ID';
frequentlyBoughtTogether({
container: '#frequentlyBoughtTogether',
recommendClient,
indexName,
objectIDs: [currentObjectID],
itemComponent({ item }) {
return html`<pre>
<code>${JSON.stringify(item)}</code>
</pre>`;
},
});
Parameters
itemComponent
|
type: ({ item }) => JSX.Element
Required
The component to display each item. |
||
container
|
type: string | HTMLElement
The container for the component. You can either pass a CSS selector or an Element. If there are several containers matching the selector, it picks up the first one. When |
||
headerComponent
|
type: (props: ComponentProps) => JSX.Element
The function to render a header for your items. The default implementation is:
Copy
|
||
fallbackComponent
|
type: () => JSX.Element
A fallback component to render when no recommendations are returned. |
||
view
|
type: ViewProps
The view component to render your items into. For example, you can use the The The default implementation is:
Copy
|
||
environment
|
type: typeof window
default: window
The environment in which your application is running. This is useful if you’re using Recommend in a different context than |
classNames
|
type: FrequentlyBoughtTogetherClassNames
The class names for the component.
Copy
|
||
translations
|
type: FrequentlyBoughtTogetherTranslations
The translations for the component.
Copy
|
This function also accepts all the props that useFrequentlyBoughtTogether
supports:
recommendClient
|
type: RecommendClient
Required
The initialized Algolia recommend client. |
indexName
|
type: string
Required
The name of the target index. |
objectIDs
|
type: string[]
Required
An array of |
maxRecommendations
|
type: number
The number of recommendations to retrieve. Depending on the available recommendations and the other request parameters, the actual number of hits may be lower than that.
If |
threshold
|
type: number
Required
Threshold for the recommendations confidence score (between 0 and 100). Only recommendations with a greater score are returned. |
queryParameters
|
type: Omit<SearchParameters, 'page' | 'hitsPerPage' | 'offset' | 'length'>
List of search parameters to send. |
transformItems
|
type: (items: Array<RecordWithObjectID<TItem>>) => items
A function to transform the retrieved items before passing them to the component. It’s useful to add or remove items, change them, or reorder them. |