Last updated

refreshCart()

Fetches the latest cart data from the server and automatically updates all active cart widget instances on the page. Use this when external changes may have affected the cart state (e.g., after user authentication, after a backend cart modification, or after a page-level event that impacts pricing/availability).

Signature

const cart = await window.FairShareSDK.refreshCart();

Parameters

None.

Returns

Promise<Cart | undefined> — Resolves to the full Cart object if a cart exists, or undefined if no cart is found.

Behavior

  1. Makes a GET request to the cart API to fetch the latest cart data
  2. Persists the response to local storage
  3. Dispatches an internal event that all mounted cart widgets consume to update their displayed state
  4. Returns the fresh cart data to the caller

Example

// Basic usage
const cart = await window.FairShareSDK.refreshCart();

// Refresh cart after authenticating a user
await window.FairShareSDK.setAuthentication(jwtToken);
const updatedCart = await window.FairShareSDK.refreshCart();

// Refresh cart after external backend modification
await fetch('/api/apply-discount', { method: 'POST', body: ... });
await window.FairShareSDK.refreshCart();

Error Handling

  • If the cart has been submitted (HTTP 410), the cart token is cleared automatically and returns undefined
  • Throws on network errors or if the SDK has not been initialized
try {
  const cart = await window.FairShareSDK.refreshCart();
} catch (error) {
  console.error('Failed to refresh cart:', error);
}

Notes

  • Requires FairShareSDK.initializeFluid() to have been called first
  • All cart widgets on the page update automatically — no additional calls needed
  • Unlike getCart, this function notifies widgets of the new data. getCart() only fetches and returns without updating widget UI.
  • getCart — Fetch cart from API (without updating widgets)
  • getLocalCart — Get cart from cache without API call