The exclude array is used to remove items from all recommendations. Its primary use is for things such as excluding cart items, excluding recently viewed items, or excluding special items that are recommended elsewhere on the page.
If
exclude
is set to a function, it will be awaited for before the primary XGen API is called.
Syntax
exclude: [prodCode1, prodCode2, prodCode3]
exclude: () => [prodCode1, prodCode2, prodCode3]
exclude: async () => [prodCode1, prodCode2, prodCode3]
Usage
The value of the exclude object should be an array
of strings
containing the product codes you want to exclude. Additionally, you can pass in a function or an async function that returns this array and that function will get called at runtime and the return value will be used. If the function is async or returns a promise, it will be awaited before calling XGen’s primary API.
Examples
With an array
exclude: ['abc123', 'def456', 'ghi789']
In this example, exclude is set to an array which means the values passed in will be excluded from all recommendations.
With a synchronous function
exclude: () => ['abc123', 'def456', 'ghi789']
In this example, exclude is set to a function and the return value of this function will be excluded from all recommendations.
With an async function
exclude: async () => {
const cartItems = fetch('/cart').then(r => r.json());
return cartItems.map(product => product.id);
}
In this example, exclude is set to an asynchronous function that calls an API. This function will be awaited and the mapped values from the API will be excluded from all recommendations.