refresh: Notify the SDK of cart changes
refresh(): Promise<void>Fetches the cart and the benefit set (the commitment charge and benefits for each range) again, recalculates the price, and updates the banner.
- When to call: Call it once right after initialization (Call it once right after initialization below), and when your code changes the cart (changing quantities, adding or removing products, applying a discount code, and so on). After the SDK adds or removes the Checkout+ product, you only fetch the cart for your UI again in
onCheckoutPlusLinesChange; you don't need to callrefresh(). - Return value:
Promise<void>. You can wait for it to complete.
Call it any time, any number of times
- Calling it more often than needed causes no problems.
refresh()changes Checkout+ product lines only on the first fetch afterinit(), to remove a Checkout+ product left over from the previous screen. Later calls torefresh()neither add nor remove Checkout+ product lines. Even if it's called whileprepareCheckout()is running, or afterprepareCheckout()has added the Checkout+ product and before navigating to checkout, the added line isn't removed.- The SDK notices by itself when the previous screen is shown as is (⑩ in What the SDK does for each trigger). It doesn't judge that from calls to
refresh(). - When the SDK adds or removes Checkout+ product lines is shown in the table in What the SDK does for each trigger.
Exceptions and failures
- Exceptions: The returned Promise never rejects.
- When the cart or the benefit set cannot be fetched: It removes the banner it was showing and reports the reason through
onError. Later calls torefresh()complete without doing anything. - After
destroy(): It does nothing and completes immediately.
Call it once right after initialization
On every screen, call refresh() once right after initialization. Screens that don't show the banner (such as a mini cart on a product page) don't call mount(), so the first fetch after init() doesn't happen there, and a Checkout+ product left over from the previous screen isn't removed (for what happens on the first fetch, see Call it any time, any number of times above).
Call it once right after each initialization. If you initialize again (call init() after destroy()), call it again right after that. You usually call init() once per page load (Step 3). In a SPA, if you don't initialize again when the screen changes, you don't need to call it again.
// Call it once right after initialization. Removes any Checkout+ product left over from a previous screen
const checkoutPlus = window.rcmr?.checkoutPlus?.init({ /* … */ }) // undefined before the SDK finishes loading. How to wait is in Step 1
void checkoutPlus?.refresh()