Home > widget-integration > RecommendationWidgetController > fetchRecommendationData

RecommendationWidgetController.fetchRecommendationData() method

Fetches recommendation data from the recommendation service.

Override this method to add shop-specific data enrichment, modify API calls, add caching strategies, or implement custom fallback logic when recommendations are unavailable.

Signature:

protected fetchRecommendationData(widgetId: string): Promise<RecommendationModel | null>;

Parameters

Parameter

Type

Description

widgetId

string

The unique identifier of the widget to fetch data for

Returns:

Promise<RecommendationModel | null>

Promise resolving to the recommendation model with widget configuration and product data, or null if no recommendations are available or fetch fails

Exceptions

{Error} When API request fails or returns invalid data (caught internally, returns null)

Example 1

Override to add product filtering based on customer segments:

protected async fetchRecommendationData(widgetId) {
  const model = await super.fetchRecommendationData(widgetId);

  if (model && this.customer) {
    // Filter out products not available in customer's region
    model.properties.products = model.properties.products.filter(
      p => p.availableRegions?.includes(this.customer.region)
    );
  }

  return model;
}

Example 2

Override to implement caching:

protected async fetchRecommendationData(widgetId) {
  const cached = this.cache.get(widgetId);
  if (cached && !this.cache.isExpired(widgetId)) {
    return cached;
  }

  const model = await super.fetchRecommendationData(widgetId);
  if (model) {
    this.cache.set(widgetId, model, { ttl: 300 }); // 5 min cache
  }
  return model;
}