cart

Read one shopping cart by id, with its loyalty and gift information folded in.

Use cart to read a cart. It only reads: adding, changing and removing items goes through the SDK (Salla.cart.addItem(id) and friends, see Salla SDK and events).

The cart id comes from the SDK in the browser, so cart.queries.currentId() reads it first and the detail query runs once it has one:

app/components/cart/CartPage.tsx
import { useQuery } from '@tanstack/react-query';
import { cart } from '@salla.sa/twilight-theme-engine/api/cart';

export function CartPage() {
  const { data: cartId } = useQuery(cart.queries.currentId());

  const { data: cartData } = useQuery({
    ...cart.queries.detail(cartId!),
    enabled: !!cartId,
  });

  return <p>{cartData ? `${cartData.items.length} items` : 'Your cart is empty'}</p>;
}

Methods

MethodParametersReturnsRequestLogin needed
getcartId: number | stringPromise<Cart | null>GET cart/{cartId}No (the cart id identifies the cart)
getOrThrowcartId: number | stringPromise<Cart | null>Same as getNo
getCurrentIdnonePromise<number | null>Asks the SDK, no requestNo

get returns the cart object with loyalty and gift set from the response (null when absent). It returns null for the whole cart once the cart has become an order.

Checkout is a separate app, so after an order the SDK can still hold the ordered cart. When get is asked for a cart the API no longer serves (no cart in the answer, a 403 or a 404) and the SDK holds that cart or none, the engine resets the SDK's cart and moves it to the customer's current one. get then returns that cart, or null when there is none: a guest, or a customer whose current cart could not be loaded. Cart badges update either way, since cart::updated fires with the new cart or with a count of 0. The engine also does this by itself when the shopper lands on the thank-you URL, in any theme. A refused cart still throws when the SDK holds a different cart.

getOrThrow turns a 404 or 410 that get throws into a not-found page and rethrows other errors. In the browser, a 404 for the cart the SDK holds is handled by get as described above, so it never becomes a not-found page. getCurrentId returns null on the server.

Queries

QueryKey
cart.queries.detail(cartId)['cart', cartId]
cart.queries.currentId()['cart', 'current-id']

Both queries use staleTime: 0 and gcTime: 0, because the SDK can change the cart from any page: a cached copy is never reused after the component unmounts. The keys include neither the locale nor the active scope.

Gotchas

  • After an SDK change (add, remove, update), refresh the cart that is on screen: queryClient.invalidateQueries({ queryKey: cart.queries.detail(cartId).queryKey }).
  • cartId is the id cart.getCurrentId() gives you, a number or a string. Do not run the detail query until you have one (enabled: !!cartId).
  • get rejects with HTTPError when the request fails. The one exception is the 403 or 404 it handles for the SDK's own cart (see Methods).
  • The id is known only in the browser, so the cart is not prefetched on the server unless your own loader has the id and calls queryClient.fetchQuery(cart.queries.detail(id)).

See also

On this page