Home > widget-integration > CartDrawerRecommendationService

CartDrawerRecommendationService class

Service for preparing cart-drawer recommendation data.

This service acts as a **shared renderer** that bridges RecommendationService to consumers (CartService for Phase 1, ThemeAppBlock for Phase 2).

## Architecture

RecommendationService (core)
        ↓
CartDrawerRecommendationService (shared renderer)
        ↓
CartService

## Key Responsibilities - Check if cart-drawer recommendation is enabled - Fetch recommendation products via RecommendationService - Format products/variants for template rendering (price formatting) - Provide data structure ready for Liquid template

## Extension Points for TS Team - formatPrice() - Customize price formatting - formatProductsForTemplate() - Customize product transformation - formatVariantsForTemplate() - Customize variant transformation - fetchRecommendationProducts() - Add custom filtering/sorting

Signature:

export declare class CartDrawerRecommendationService 

Example 1

Extension point for TS team - custom price formatting:

window.boostWidgetIntegration.extend('CartDrawerRecommendationService', (Base) => {
  return class extends Base {
    formatPrice(price, showCurrencyCodes) {
      // Custom formatting with EUR currency
      return `€${(price / 100).toFixed(2)}`;
    }
  };
});

Example 2

Extension point - filter out-of-stock products:

window.boostWidgetIntegration.extend('CartDrawerRecommendationService', (Base) => {
  return class extends Base {
    formatProductsForTemplate(products, showCurrencyCodes) {
      // Filter out products with no available variants
      const availableProducts = products.filter(p =>
        p.variants?.some(v => v.available)
      );
      return super.formatProductsForTemplate(availableProducts, showCurrencyCodes);
    }
  };
});

Constructors

Constructor

Modifiers

Description

(constructor)(recommendationService, appService)

Constructs a new instance of the CartDrawerRecommendationService class

Properties

Property

Modifiers

Type

Description

appService

protected

AppService

recommendationService

protected

RecommendationService

Methods

Method

Modifiers

Description

fetchRecommendationProducts(widgetId)

protected

Fetch recommendation products from API via RecommendationService.

Override this method to: - Add custom filtering before/after fetch - Pass additional context (cart product IDs, customer ID) - Implement caching

filterAvailableProducts(products)

protected

Filter out products that are completely out of stock.

Removes products where no variants are available (i.e., all variants are OOS with "sell when out of stock" disabled). Products with at least one available variant are kept.

Override this method to customize product availability filtering logic.

formatPrice(price, showCurrencyCodes)

protected

Format a price value for display.

Uses AppService.formatCurrency for consistent store-wide formatting. Override this method to customize price formatting.

formatProductsForTemplate(products, showCurrencyCodes)

protected

Format products for template rendering.

Transforms raw API products into template-ready format with: - Formatted prices (using store currency settings) - Formatted variant prices - Consistent structure for Liquid template

formatVariantsForTemplate(variants, showCurrencyCodes)

protected

Format variants for template rendering.

Transforms raw variant data into display-ready format with formatted prices. Override to add custom variant properties or filtering logic.

getCartDrawerRecommendationData(showCurrencyCodes)

Get cart-drawer recommendation data ready for template rendering.

This is the main method consumers (CartService, ThemeAppBlock) should call. It handles the full flow: validate → fetch → format → return.

getCartSettings()

protected

Get cart settings from TAE config.

Override this method to provide custom cart settings or merge additional shop-specific configuration.

getWidgetConfig(widgetId)

protected

Get widget configuration from TAE recommendationWidgets.

Override this method to provide custom widget configurations or fallback values for specific shops.

getWidgetId()

Get the widget ID from cart settings.

isEnabled()

Check if cart-drawer recommendation is enabled in TAE config.

Checks for: - cart.enableWidgetRecommendation = true - cart.widgetRecommendationId is set

isPreviewMode()

protected

Whether the widget is in preview mode (e.g., TAE Admin preview). Override to check TAE preview state or enable preview for specific conditions.

parsePrice(price)

protected

Parse a price value from API response.

The API may return prices as numbers or strings (e.g., "20.0"). This method normalizes them to numbers for formatting.

sortVariantsByAvailability(variants)

protected

Sort variants so that available (in-stock) variants come first.

This ensures the default variant selection (first in the list) prioritizes in-stock variants, preventing users from being defaulted to an OOS variant.

Override this method to customize variant sort order.