Home > widget-integration > RecommendationService > getRecentlyPurchasedProductIds
RecommendationService.getRecentlyPurchasedProductIds() method
Retrieves product IDs for recently purchased products by the customer.
This method can be overridden to customize which recently purchased products should be used for recommendations (e.g., filtering by time period, product type, or purchase frequency).
Signature:
protected getRecentlyPurchasedProductIds(): Promise<(string | number)[]>;
Returns:
Promise<(string | number)[]>
Promise resolving to array of product IDs (string or number) from the customer's purchase history. Returns empty array if no customer or no purchase history.
Example
Override to filter by purchase date:
protected async getRecentlyPurchasedProductIds(): Promise<(string | number)[]> {
const customerId = this.platformLoader.platform.customer?.id;
if (!customerId) return [];
// Get products purchased in the last 30 days
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
const orders = await this.getCustomerOrders(customerId, thirtyDaysAgo);
return orders.flatMap(order => order.line_items.map(item => item.product_id));
}