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.
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>;
}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
| Method | Parameters | Returns | Request | Login needed |
|---|---|---|---|---|
list | params?: OrdersListParams | Promise<{ data: OrderListItem[]; cursor?: Pagination }> | GET orders?page&cursor&status&feedback_status&with_items=1 | Yes |
find | id: string | Promise<{ order: Order }> | GET orders/{id} | Yes |
findOrThrow | id: string | Promise<{ order: Order }> | Same as find | Yes |
shipments | orderId: string | Promise<OrderShipmentItem[]> | GET orders/{orderId}/shipments | Yes |
ratings | orderId: string | Promise<OrderRatingItem[]> | GET orders/{orderId}/ratings | Yes |
thankYou | orderId: string | Promise<CheckoutThankYouData> | GET checkout/thank-you/{orderId} | Not checked by the module |
thankYouOrThrow | orderId: string | Promise<CheckoutThankYouData> | Same as thankYou | Not 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
| Query | Key |
|---|---|
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 withisLoggedIn. listturns anulldatainto[];cursormay be absent. Passcursor.nextback ascursor.findOrThrowturns 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 400token_not_providedis neither, so it still reaches the error boundary.thankYouOrThrowturns a 404 or 410 into not-found. See Errors.shipmentsandratingsreturn[]when the response has nodata.- Orders are read-only here. Invalidate
['orders']after an SDK action that changes one, such as a rating or a cancel.