UNPKG

@klevu/core

Version:

Typescript SDK that simplifies development on Klevu backend. Klevu provides advanced AI-powered search and discovery solutions for online retailers.

44 lines (43 loc) 1.63 kB
import { KlevuTypeOfRecord, KlevuTypeOfRequest, } from "../../index.js"; import { KlevuLastClickedProducts } from "../../store/lastClickedProducts.js"; const defaultOptions = { limit: 5, lastClickedProductIds: [], }; /** * Shows products that visitor should also see. Automatically applies products that user has already clicked. * * @category RecommendationQuery * @param {Options} options Allows to override limit of products to return or pass custom lastClickedProductIds * @returns */ export function alsoViewed(options, ...modifiers) { const params = Object.assign(Object.assign({}, defaultOptions), options); const lastProducts = params.lastClickedProductIds && params.lastClickedProductIds.length > 0 ? params.lastClickedProductIds : KlevuLastClickedProducts.getLastClickedLatestsFirst(); return { klevuFunctionId: "recommendation", modifiers, queries: [ { id: "alsoviewed", typeOfRequest: KlevuTypeOfRequest.AlsoViewed, settings: { limit: params.limit, context: { recentObjects: [ { typeOfRecord: KlevuTypeOfRecord.Product, records: lastProducts.map((id) => ({ id, })), }, ], }, typeOfRecords: [KlevuTypeOfRecord.Product], }, }, ], }; }