Retrieves the recommended items provided by your plugin.
The getRecommendations()
function is called by Wix to gather the recommended items based on the selected algorithm. It returns recommendations based on the specified options.
getRecommendations()
When you add the Recommendations service plugin,
a folder is automatically added to your site. Use the .js
file in the folder to write the code to determine which recommended items to retrieve.
For more information on customizing your recommendations plugin, see Recommendations Service Plugin.
function getRecommendations(
options: Options,
context: Context,
): Promise<GetRecommendationsResponse>;
Recommendation options.
Metadata about the request.
This example retrieves recommended items based on the selected algorithm.
import wixData from "wix-data";
/**
* Returns lists of items recommended by each requested algorithm.
*
* If the algorithm's type is `RELATED_ITEMS` then the `items` field is required in the request, and the response will contain items related to those submitted.
* How the related items are selected based on the requested items depends on the algorithm. Some examples include items related by category or those frequently bought or watched together.
* @param {import('interfaces-ecom-v1-recommendations-provider').GetRecommendationsOptions} options
* @param {import('interfaces-ecom-v1-recommendations-provider').Context} context
* @returns {Promise<import('interfaces-ecom-v1-recommendations-provider').GetRecommendationsResponse | import('interfaces-ecom-v1-recommendations-provider').BusinessError>}
*/
export const getRecommendations = async (options, context) => {
const recommendations = await Promise.all(
options.algorithmIds.map((algorithmId) => {
if (algorithmId == "97f48eb2-4d42-4296-b620-13862dd0353e") {
return wixData
.query("Stores/Products")
.ascending("price")
.limit(10)
.find()
.then((results) => {
return {
algorithmId: algorithmId,
recommendedItems: results.items.map((item) => {
return {
catalogItemId: item._id,
appId: "215238eb-22a5-4c36-9e7b-e7c08025e04e",
};
}),
};
});
}
}),
);
return {
recommendationPerAlgorithm: recommendations,
};
};
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.