Setting the file. One moment.
Wix Restaurants Ordering · Wix Vibe Headless · wix/skills · Skills Docs
ContentsBack to the top of the page Wix Blog
Previous
Reference Wix Restaurants Menu
references/restaurants/app/rest/ wix-restaurants-ordering.js
JavaScript · 156 lines · 7 KB
14
* Cart (Wix eCom) — restaurant orders use the same visitor cart as the storefront.
15 * id {string}, currency {string},
16 * lineItems[].id {string} — lineItemId for update/remove (NOT the item id),
17 * lineItems[].quantity {number},
18 * lineItems[].catalogReference.catalogItemId {string} — the ordered item's GUID,
19 * lineItems[].productName.original {string},
20 * lineItems[].price.formattedAmount {string},
21 * lineItems[].availability.status {string} — "AVAILABLE"|"NOT_AVAILABLE"|"PARTIALLY_AVAILABLE"|"NOT_FOUND"
22 * Full model: https://dev.wix.com/docs/api-reference/business-solutions/e-commerce/cart/cart-object.md
23 */
24
25 // Wix Restaurants Orders app id — required in catalogReference when adding menu items to the eCom cart.
26 const RESTAURANTS_ORDERS_APP_ID = "9a5d83fd-8570-482e-81ab-cfa88942ee60" ;
27
28 /**
29 * List the restaurant's online-ordering operations.
30 * GET https://www.wixapis.com/restaurants-operations/v1/operations
31 * @returns {Promise<object[]>}
32 */
33 export async function listOperations () {
34 const res = await wixApiRequest ( "/restaurants-operations/v1/operations" , { method: "GET" });
35 return res?.operations ?? [];
36 }
37
38 /**
39 * Pick the operation to order through — the default, else the first ENABLED, else the first.
40 * Returns null when no operation is configured (show an "ordering unavailable" state).
41 * @returns {Promise<object|null>}
42 */
43 export async function getDefaultOperation () {
44 const operations = await listOperations ();
45 return (
46 operations. find (( o ) => o.default) ??
47 operations. find (( o ) => o.onlineOrderingStatus === "ENABLED" ) ??
48 operations[ 0 ] ??
49 null
50 );
51 }
52
53 /**
54 * Add a restaurant menu item to the visitor's current eCom cart.
55 *
56 * Requires operationId (from listOperations/getDefaultOperation), menuId, and sectionId.
57 * Throws if any is missing, or if the added line is not AVAILABLE.
58 *
59 * Variant selection and modifier up-charges on the cart line are not covered here — the
60 * restaurants catalogReference.options shape for those is not documented for client add-to-cart.
61 * Confirm the shape before extending:
62 * https://dev.wix.com/docs/api-reference/business-solutions/restaurants/online-orders/sample-flows.md
63 *
64 * POST https://www.wixapis.com/ecom/v1/carts/current/add-to-cart
65 * @param {string} itemId Menu item GUID (item.id).
66 * @param {{ operationId: string, menuId: string, sectionId: string, onlineOrderingPageUrl?: string, quantity?: number }} opts
67 * @returns {Promise<object>} Updated cart.
68 */
69 export async function addItemToCart ( itemId , { operationId , menuId , sectionId , onlineOrderingPageUrl , quantity = 1 } = {}) {
70 if ( ! itemId) throw new Error ( "addItemToCart: itemId is required." );
71 if ( ! operationId || ! menuId || ! sectionId) {
72 throw new Error ( "addItemToCart: operationId, menuId and sectionId are all required for a restaurant line item." );
73 }
74 const options = { operationId, menuId, sectionId };
75 if (onlineOrderingPageUrl) options.onlineOrderingPageUrl = onlineOrderingPageUrl;
76
77 const res = await wixApiRequest ( "/ecom/v1/carts/current/add-to-cart" , {
78 method: "POST" ,
79 body: {
80 lineItems: [{ catalogReference: { appId: RESTAURANTS_ORDERS_APP_ID , catalogItemId: itemId, options }, quantity }],
81 },
82 });
83 const line = (res?.cart?.lineItems ?? []). find (( l ) => l.catalogReference?.catalogItemId === itemId);
84 if (line?.availability?.status && line.availability.status !== "AVAILABLE" ) {
85 throw new Error ( `Item not available to order (status: ${ line . availability . status }).` );
86 }
87 return res?.cart;
88 }
89
90 /** Read the visitor's current cart. Returns null if no cart exists yet. */
91 export async function getCurrentCart () {
92 try {
93 const res = await wixApiRequest ( "/ecom/v1/carts/current" , { method: "GET" });
94 return res?.cart ?? null ;
95 } catch {
96 return null ;
97 }
98 }
99
100 /**
101 * Update the quantity of a cart line. lineItemId is cart.lineItems[].id, not the item id.
102 * POST https://www.wixapis.com/ecom/v1/carts/current/update-line-items-quantity
103 * @returns {Promise<object>} Updated cart.
104 */
105 export async function updateCartItemQuantity ( lineItemId , quantity ) {
106 const res = await wixApiRequest ( "/ecom/v1/carts/current/update-line-items-quantity" , {
107 method: "POST" ,
108 body: { lineItems: [{ id: lineItemId, quantity }] },
109 });
110 return res?.cart;
111 }
112
113 /**
114 * Remove a line from the current cart by its cart.lineItems[].id.
115 * POST https://www.wixapis.com/ecom/v1/carts/current/remove-line-items
116 * @returns {Promise<object>} Updated cart.
117 */
118 export async function removeFromCart ( lineItemId ) {
119 const res = await wixApiRequest ( "/ecom/v1/carts/current/remove-line-items" , {
120 method: "POST" ,
121 body: { lineItemIds: [lineItemId] },
122 });
123 return res?.cart;
124 }
125
126 /**
127 * Create a checkout from the current cart and return the Wix-hosted checkout URL.
128 * Throws on empty cart, unavailable lines, or a missing redirect URL.
129 * Redirect with: window.location.href = await checkout()
130 * @returns {Promise<string>}
131 */
132 export async function checkout () {
133 const cart = await getCurrentCart ();
134 const lines = cart?.lineItems ?? [];
135 if ( ! lines. length ) throw new Error ( "Cannot check out: the cart is empty." );
136 const unavailable = lines. filter (( l ) => l.availability?.status && l.availability.status !== "AVAILABLE" );
137 if (unavailable. length ) {
138 const names = unavailable. map (( l ) => l.productName?.original ?? l.catalogReference?.catalogItemId). join ( ", " );
139 throw new Error ( `Cannot check out: ${ unavailable . length } item(s) not available — ${ names }.` );
140 }
141
142 const checkoutRes = await wixApiRequest ( "/ecom/v1/carts/current/create-checkout" , {
143 method: "POST" ,
144 body: { channelType: "WEB" },
145 });
146 const checkoutId = checkoutRes?.checkoutId;
147 if ( ! checkoutId) throw new Error ( "Failed to create checkout from the current cart." );
148
149 const redirect = await wixApiRequest ( "/headless/v1/redirect-session" , {
150 method: "POST" ,
151 body: { ecomCheckout: { checkoutId }, callbacks: { postFlowUrl: window.location.href } },
152 });
153 const url = redirect?.redirectSession?.fullUrl;
154 if ( ! url) throw new Error ( "Failed to create the checkout redirect session." );
155 return url;
156 }