With the Spring ’21 release, customers can use a Lightning Message Service (LMS) channel to uniformly publish and subscribe to notifications about changes to the state of their Commerce cart. The LMS channel “lightning__commerce_cartChanged” lets customers notify every out-of-the-box Commerce component when the cart has been modified. This notification triggers all relevant components to refresh their data accordingly. The LMS channel is the preferred long-term solution for custom component integration with OOTB components.
Support for the “cartchanged” DOM event will be removed in a future release (Safe Harbor).
Refer to the Lightning Message Service documentation for more details and in-depth guidance.
import { LightningElement, wire } from "lwc";
import {
publish,
MessageContext
} from "lightning/messageService";
import cartChanged from "@salesforce/messageChannel/lightning__commerce_cartChanged";
export default class CartUpdatingComponent extends LightningElement {
@wire(MessageContext)
messageContext;
/**
* Handles a user button click to add a product to their cart.
*/
addToCart(event) {
addToCartAsync(...)
.then(() => {
publish(this.messageContext, cartChanged);
});
}
}
this.dispatchEvent(new CustomEvent("cartchanged", {
bubbles: true,
composed: true
}));
Support for triggering a refresh event on the cart badge Lightning Web Component (LWC) is already in place. So firing an event, which this snippet does, triggers the desired refresh.
In a store, you can run the code snippet right from the browser debugger console. You can tell that the snippet is working when the console shows a network call to get cart information.
In the future (safe harbor applies) we'll be working to include a supported way for customers to trigger these kinds of updates natively.
/**
* Handles a user button click to add a product to their cart.
*/
addtoCart() {
addToCartAsync(...)
.then(result => {
this.dispatchEvent(new CustomEvent("cartchanged", {
bubbles: true,
composed: true
}));
});
}
000389948

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.