order

List the customer's orders, read one order with its shipments and ratings, and load the checkout thank-you data.

Use order for the account order list, a single order page and the thank-you page after checkout. Orders belong to the signed-in customer, so gate calls with useUser().isLoggedIn.

app/components/MyOrders.tsx
import { useQuery } from '@tanstack/react-query';
import { useUser } from '@salla.sa/twilight-theme-engine/hooks/useUser';
import { order } from '@salla.sa/twilight-theme-engine/api/order';

export function MyOrders() {
  const { isLoggedIn } = useUser();
  const { data } = useQuery({
    ...order.queries.list({ with_items: true }),
    enabled: isLoggedIn,
  });
  const orders = data?.data ?? [];

  return <p>{orders.length} orders</p>;
}
app/routes/account.orders.tsx
import { order } from '@salla.sa/twilight-theme-engine/api/order';
import { getTwilightContext } from '@salla.sa/twilight-theme-engine/tanstack';

loader: () => {
  const { queryClient } = getTwilightContext();
  return queryClient.fetchQuery(order.queries.list({ status: 'completed' }));
},

Methods

MethodParametersReturnsRequestLogin needed
listparams?: OrdersListParamsPromise<{ data: OrderListItem[]; cursor?: Pagination }>GET orders?page&cursor&status&feedback_status&with_items=1Yes
findid: stringPromise<{ order: Order }>GET orders/{id}Yes
findOrThrowid: stringPromise<{ order: Order }>Same as findYes
shipmentsorderId: stringPromise<OrderShipmentItem[]>GET orders/{orderId}/shipmentsYes
ratingsorderId: stringPromise<OrderRatingItem[]>GET orders/{orderId}/ratingsYes
thankYouorderId: stringPromise<CheckoutThankYouData>GET checkout/thank-you/{orderId}Not checked by the module
thankYouOrThroworderId: stringPromise<CheckoutThankYouData>Same as thankYouNot checked by the module

OrdersListParams: page, cursor, status, feedback_status, with_items (adds each order's items) and signal. "Login needed" means the API wants the customer's token; the module does not check it for you.

Queries

QueryKey
order.queries.list(params?)['orders', 'list', params]
order.queries.find(id)['orders', 'detail', id]
order.queries.shipments(orderId)['orders', 'shipments', orderId]
order.queries.ratings(orderId)['orders', 'ratings', orderId]
order.queries.thankYou(orderId)['orders', 'thankYou', orderId]

No key includes the locale, the scope or the customer. The token is what separates customers, so clear ['orders'] on logout so the next customer does not see cached data.

Gotchas

  • Guests get an error from the API (token_not_provided). Skip the call with isLoggedIn.
  • list turns a null data into []; cursor may be absent. Pass cursor.next back as cursor.
  • findOrThrow turns a 401 or 403 into the unauthorized page and a 404 or 410 (an order the customer cannot see) into the not-found page, and lets other errors through. A guest's 400 token_not_provided is neither, so it still reaches the error boundary. thankYouOrThrow turns a 404 or 410 into not-found. See Errors.
  • shipments and ratings return [] when the response has no data.
  • Orders are read-only here. Invalidate ['orders'] after an SDK action that changes one, such as a rating or a cancel.

See also

On this page