I am trying to implement a custom checkout flow in which account customers can place an order without having to pay and receive confirmation like any other order.
I have attached my current solution below which is temperamental... It was successfully creating orders consistently but now I get a selectedCarrierServiceOption not found error, despite the choice being the same hard-coded value as when the function was working.
I am now also observing this: "Error: Got (400) when requested: /_api/wixstores-graphql-server/graphql" when I try and click the cart option in my top nav bar to open the side cart.
And the primary issue that I have been facing through the process is, when my method does successfully create an order, there is no update to the Wix Stores "Orders" collection in the CMS, but it does updated the inventory qty of the products in the order.
Please see below my code and feel free to ask any questions for clarification. Thanks in advance for any help!
import { Permissions, webMethod } from "wix-web-module";
import { checkout, currentCart} from "wix-ecom-backend";
import { elevate } from "wix-auth";
export const myCreateCheckoutFunction = webMethod(
Permissions.Anyone,
async () => {
try {
const cart = await currentCart.getCurrentCart()
const checkoutId = cart.checkoutId
const newCheckout = await checkout.getCheckout(checkoutId)
console.log("Success! Checkout retrieved, checkout:", newCheckout);
const elevatedCreateOrder = elevate(checkout.createOrder)
// Update shipping info to include selectedCarrierServiceOption
const updatedCheckout = await checkout.updateCheckout(checkoutId, {
shippingInfo: {
selectedCarrierServiceOption: {
code: "2f8d9011-bff8-4d5f-9620-52e75ffbc8a6",
}
}
});
// Create new order from updated checkout
const newOrder = await elevatedCreateOrder(updatedCheckout._id)
console.log("Success! new order created: ", newOrder)
// Mark checkout as complete after order placed
await checkout.markCheckoutAsCompleted(updatedCheckout._id)
console.log("Success! Checkout completed.")
return newOrder;
} catch (error) {
console.error(error);
console.error("Error in creating checkout/order backend: ", error)
}
},
);