Home > widget-integration > CartDrawerRecommendationController > handleAddToCart
CartDrawerRecommendationController.handleAddToCart() method
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
Signature:
protected handleAddToCart(productId: string, variantId: string, buttonElement: HTMLElement): Promise<void>;
Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
productId |
string |
The product ID to add |
|
variantId |
string |
The variant ID to add |
|
buttonElement |
HTMLElement |
The clicked button element (for loading state) |
Returns:
Promise<void>
Example
Override to add custom validation or quantity logic before adding to cart:
window.boostWidgetIntegration.extend('CartDrawerRecommendationController', (Base) => {
return class extends Base {
async handleAddToCart(productId, variantId, buttonElement) {
// Example: add custom quantity or properties
const quantity = this.getCustomQuantity(productId);
if (quantity > 0) {
await super.handleAddToCart(productId, variantId, buttonElement);
}
}
};
});