Home > widget-integration > CartService > handlePostAddToCartAction
CartService.handlePostAddToCartAction() method
Handle post add-to-cart actions (redirect, drawer, notification, etc.) Override to customize behavior after successfully adding items to cart
Common customization scenarios: - Show cart drawer/modal instead of redirect - Display success notification - Trigger upsell/cross-sell modal - Update mini cart display - Track add-to-cart events in analytics
Default behavior: - If cart drawer is enabled: do nothing (let drawer handle it) - If cart drawer is disabled: redirect to cart page
Signature:
protected handlePostAddToCartAction(): Promise<void>;
Returns:
Promise<void>
Promise that resolves when post-add action is complete
Example 1
Show cart drawer
protected async handlePostAddToCartAction(): Promise<void> {
const { enableCart, showNotification } = this.appService.themeSettings.cart;
if (enableCart) {
// Show cart drawer
await this.showCartDrawer();
} else if (showNotification) {
// Show success notification without redirect
this.showSuccessNotification('Item added to cart!');
} else {
// Default: redirect to cart page
this.redirectToCartPage();
}
}
Example 2
Track analytics and show upsell
protected async handlePostAddToCartAction(): Promise<void> {
// Track add-to-cart event
await this.trackAddToCartEvent();
// Show upsell recommendations
const showUpsell = this.appService.getConfig().upsell?.enabled;
if (showUpsell) {
await this.showUpsellModal();
return; // Don't redirect if showing upsell
}
// Default behavior
await super.handlePostAddToCartAction();
}