Home > widget-integration > CartDrawerRecommendationController
CartDrawerRecommendationController class
Controller for managing recommendation widget interactions within the cart drawer.
NOTE: This controller does NOT extend RecommendationWidgetController to avoid circular dependency (RecommendationModule imports CartModule). Instead, it implements its own formatPrice() method using the same logic as the base controller.
This controller handles user interactions with recommendation products that are rendered by the backend cart template. The template renders the recommendation widget using data from CartService.getCartDrawerRecommendationData().
## Key Responsibilities - Handles "Add to Cart" button clicks from recommendation products - Manages native CSS/JS carousel (not Slick) for recommendation products - Dispatches cart updated events after adding products - Manages event listeners for cart drawer lifecycle
## Extension Points - getAddToCartButtonSelector() - Customize the Add to Cart button selector - getRecommendationContainerSelector() - Customize the recommendation container selector - handleAddToCart() - Customize add to cart behavior - afterCartDrawerAddToCart() - Add custom post-add-to-cart logic - getSlidesPerView() - Customize number of slides per view based on cart style - getHighlightDuration() - Customize success/error message display duration - getErrorMessage() - Customize error message extraction for shop-specific errors - formatPrice() - Customize price formatting for variant price updates - getAddingButtonText() / getAddedButtonText() / getFailedButtonText() / getDefaultAddButtonText() / getSuccessMessageText() - Customize button and message strings for localization
Signature:
export declare class CartDrawerRecommendationController
Example
Extend to customize add to cart behavior:
window.boostWidgetIntegration.extend('CartDrawerRecommendationController', (Base) => {
return class CustomCartDrawerRec extends Base {
async handleAddToCart(productId, variantId) {
// Custom logic before adding to cart
await super.handleAddToCart(productId, variantId);
// Custom logic after adding to cart
}
};
});
Constructors
|
Constructor |
Modifiers |
Description |
|---|---|---|
|
(constructor)(appService, cartAPI, cartDrawerRecommendationService, constants) |
Constructs a new instance of the |
Properties
|
Property |
Modifiers |
Type |
Description |
|---|---|---|---|
|
| |||
|
|
((event: Event) => void) | null | ||
|
|
((event: Event) => void) | null | ||
|
|
((event: Event) => void) | null | ||
|
|
(() => void) | null | ||
|
| |||
|
|
() => void | ||
|
| |||
|
|
() => void | ||
|
| |||
|
| |||
|
|
() => void |
Handle mouse drag end. | |
|
|
(event: MouseEvent) => void |
Handle mouse drag move. | |
|
|
(event: MouseEvent) => void |
Handle mouse drag start. | |
|
|
() => void |
Handle click on next button. Slides by the number of products per row (slidesPerView). | |
|
|
() => void |
Handle click on previous button. Slides by the number of products per row (slidesPerView). | |
|
|
() => void |
Handle touch end. | |
|
|
(event: TouchEvent) => void |
Handle touch move. | |
|
|
(event: TouchEvent) => void |
Handle touch start. | |
|
|
boolean |
Whether the controller has been initialized with click handlers. | |
|
|
boolean | ||
|
|
CarouselState | null |
Methods
|
Method |
Modifiers |
Description |
|---|---|---|
|
|
Called after a product is successfully added to cart from cart drawer. Override to add custom post-add-to-cart logic such as analytics tracking, notification display, or triggering shop-specific side effects. | |
|
|
Apply or remove the two-columns layout modifier class on the recommendation container. This class enables CSS grid styling for the two-columns layout on desktop. On mobile/tablet the modifier is removed so the carousel layout takes effect. Called on cart open, after re-fetch render, and on window resize. | |
|
|
Add the disabled class to the slider element when carousel is not initialized. This signals via CSS that the slider navigation and scroll behavior are inactive. | |
|
|
Check if all products in the recommendation widget are hidden. Checks both the synchronous | |
|
|
Clear error message from a recommendation item. | |
|
|
Clear success message from a recommendation item. | |
|
Full cleanup of all event listeners and resources. Removes click/change handlers, cart drawer open/close listeners, and destroys the native carousel. Call this when the module is being destroyed and the controller should no longer respond to any events. | ||
|
|
Destroy the native carousel and clean up resources. | |
|
|
Finalize drag interaction and snap to nearest slide. | |
|
Formats a price value according to shop settings and theme configuration. This method implements the same logic as RecommendationWidgetController.formatPrice() but is duplicated here to avoid circular dependency issues. | ||
|
|
Format price if it appears to be a raw number string. If the price already contains currency symbols or non-numeric characters, it's considered already formatted and returned as-is. Uses the local | |
|
|
Get the button text displayed after successfully adding a product to cart. Override this method to customize success button text for localization or shop-specific messaging. | |
|
|
Get the button text displayed while adding a product to cart. Override this method to customize button text for localization or shop-specific messaging. | |
|
|
Get the CSS selector for Add to Cart buttons in cart drawer recommendations. Override this method to use a different button selector. | |
|
|
Get the quantity to add when clicking the Add to Cart button. Override to implement quantity pickers or per-product custom quantities. | |
|
|
Get bounded translateX value within carousel limits. | |
|
|
Get the delay in milliseconds before resetting the Add to Cart button text after success or error. Override to adjust how long the success/error button state remains visible before returning to default. | |
|
|
Get the CSS selector for the cart drawer modal. Override this method if your theme uses a different cart drawer structure. | |
|
|
Get the current translateX value from the container element. | |
|
|
Get the default button text for the Add to Cart button. Override this method to customize default button text for localization or shop-specific messaging. | |
|
|
Extract a user-friendly error message from an error. Override to customize error messages for shop-specific error patterns or to add localization support. | |
|
|
Get the button text displayed when adding a product to cart fails. Override this method to customize error button text for localization or shop-specific messaging. | |
|
|
Get highlight duration in milliseconds for success/error states. Override to adjust how long success and error messages remain visible. | |
|
|
Get the image width for recommendation product images. Checks widget design settings first, then falls back to default. Override this method to customize image width calculation. | |
|
|
Get the CSS selector for the next button. | |
|
|
Get the CSS selector for the previous button. | |
|
|
Get the number of variants for a product from its DOM element. Checks data attribute or variant selector options. | |
|
|
Get the CSS selector for the recommendation container in cart drawer. Override this method to use a different container selector. | |
|
|
Get the CSS selector for the slider track (container of slides). | |
|
|
Get the CSS selector for the slider container. Override this method to use a different slider selector. | |
|
|
Get the CSS selector for individual slides. | |
|
|
Get the number of slides to show per view based on cart style. Uses Override this method to customize slides per view calculation. | |
|
|
Get the success message text displayed after adding a product to cart. Override this method to customize success message for localization or shop-specific messaging. | |
|
|
Get the CSS selector for variant select dropdowns. | |
|
|
Extract widget design settings from the recommendation container data attributes or from the TAE config (same approach as other recommendation widgets like volume-bundle). | |
|
|
Handles adding a product to cart from recommendation widget. Optimization: Does NOT re-fetch recommendation API after add to cart. - If product has 0-1 variants: hides the product from widget (DOM only) - If all products are hidden: re-fetches API to get new recommendations - If product has 2+ variants: keeps it visible for user to add other variants | |
|
|
Hide the recommendation block when it contains no product items. Prevents showing an empty/broken layout in the cart drawer. | |
|
|
Hide a product from the recommendation widget. Uses CSS display:none for immediate visual feedback. | |
|
|
Hide the entire recommendation widget when no products available. | |
|
|
Initialize the native CSS/JS carousel for recommendation products. Sets up slide dimensions, navigation buttons, and initial position. | |
|
|
Determine whether the current layout is the two-columns grid layout. Returns | |
|
|
Re-fetch recommendation data and re-render the widget. Called when all products have been hidden after add-to-cart actions. | |
|
|
Removes cart drawer event listeners. | |
|
|
Removes click and change handlers from the cart drawer. Called when the cart drawer is closed. | |
|
|
Remove the resize handler. | |
|
|
Remove the disabled class from the slider element when carousel is initialized. | |
|
|
Removes the delegated change handler for variant selects. Called when the cart drawer is closed. | |
|
|
Render the recommendation widget with new data. Override this method to customize rendering. Returns a Promise that resolves when the DOM update is complete. The consumer listening to 'boost-sd-rcu-refetch' should dispatch 'boost-sd-rcu-render-complete' after finishing the DOM update. | |
|
|
Sets up event listeners for cart drawer open/close events. | |
|
|
Sets up click handlers for Add to Cart buttons in recommendations. Called when the cart drawer is opened. | |
|
|
Setup click handlers for carousel navigation buttons. | |
|
|
Setup drag and touch handlers for carousel swipe functionality. | |
|
|
Setup a debounced resize handler that reinitializes the carousel when the window size changes (e.g., viewport rotation, drawer width change). | |
|
|
Setup change handlers for variant select dropdowns. Uses event delegation on the cart drawer container to avoid memory leaks. Updates the product item's data-variant-id when variant selection changes. | |
|
|
Determine whether the native carousel should be initialized. Returns Override this method to add custom conditions for carousel initialization. | |
|
|
Show an error message on a recommendation item. | |
|
|
Show a success message in the recommendation widget after adding to cart. | |
|
|
Update carousel state after a product has been removed. Recalculates slide positions and updates navigation. | |
|
|
Update carousel navigation button visibility/state. Hides prev button at start, hides next button at end. | |
|
|
Update the carousel position by translating the container. | |
|
|
Update the product image when variant changes. Override to customize image update behavior. | |
|
|
Update the product price when variant changes. Override to customize price update behavior. Uses the local |