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:
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
| Method | Parameters | Returns | Request | Login needed |
|---|---|---|---|---|
get | cartId: number | Promise<Cart | null> | GET cart/{cartId} | No (the cart id identifies the cart) |
getOrThrow | cartId: number | Promise<Cart | null> | Same as get | No |
getCurrentId | none | Promise<number | null> | Asks the SDK, no request | No |
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. getOrThrow turns a 404 or 410 into a not-found page and rethrows other errors. getCurrentId returns null on the server.
Queries
| Query | Key |
|---|---|
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 }). cartIdis a number. Do not run the detail query until you have one (enabled: !!cartId).getdoes not catch errors: an expired or unknown cart rejects withHTTPError.- 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)).