I am new to using Shopify APIs. I am trying to create a custom order form (something like this - https://getchongcbd.com/good-vibes-cart/) in builder.io page builder (https://exploreforia.com/trial), where the user will select one of the 2 products; enter shipping details in a form. On submit, these details should be sent to the Shopify checkout page and s/he should be directed to the checkout page showing all these details. For example, it can be any of these pages - https://prnt.sc/e9ng-P83fJ9s or https://prnt.sc/bp63GawcKUS-
Here is some of the code I have written on the Submit button.
event.preventDefault();
const userName = event.target[0].value;
const email = event.target[1].value;
const productId = event.target[2].value;
alert(userName);
alert(email);
alert(productId);
// Example code to create a Shopify checkout using the Fetch API
const createShopifyCheckout = async () => {
const shopifyAPIEndpoint = 'https://koryart.myshopify.com/admin/api/2023-07/checkouts.json';
try {
const response = await fetch(shopifyAPIEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': '0f71xxxxxxxxxxxxxxxxxxxx5530', // Replace with your Shopify access token
},
body: JSON.stringify({
// Include the line items and other necessary data for your order
lineItems: [
{
variantId: productId,
quantity: 1,
},
// Add more items as needed
],
}),
});
if (response.ok) {
const data = await response.json();
alert(data);
const checkoutUrl = data.data.checkoutCreate.checkout.webUrl;
alert(checkoutUrl);
// Now you can redirect the user to the checkout URL
window.location.href = checkoutUrl;
} else {
console.error('Failed to create Shopify checkout:', response.statusText);
}
} catch (error) {
console.error('An error occurred:', error);
}
};
// Call the function when the form is submitted
document.getElementById('your-form-id').addEventListener('submit', (e) => {
e.preventDefault();
createShopifyCheckout();
});
This code doesn’t do anything and doesn’t go anywhere.
I am not sure I am using the correct API / mutant as this API only accept product information whereas i want to send both product as well as shipping information such as name, email, address, city, state, zipcode, etc.
Can someone help me to figure out whether I am using the correct API or where am I erring?
Thanks in advance.
Ketan Shah