Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hi,
We're creating a landing page for a marketing partner. We'll add a button named "Add to Cart" in the landing page. How to add an item to the cart programmatically, from this page? Is it done the way explained at this Shopify dev doc https://shopify.dev/docs/api/ajax/reference/cart
Solved! Go to the solution
This is an accepted solution.
et the cart token or create a new cart if one doesn't exist yet. You can use the AJAX API endpoint '/cart.js' with the HTTP method GET to retrieve the cart token or POST to create a new cart.
Use the AJAX API endpoint '/cart/add.js' with the HTTP method POST to add an item to the cart. Pass the variant ID and the quantity as parameters in the request payload.
Example payload:
{
items: [
{
id: 'variant_id',
quantity: 1
}
]
}
This is an accepted solution.
et the cart token or create a new cart if one doesn't exist yet. You can use the AJAX API endpoint '/cart.js' with the HTTP method GET to retrieve the cart token or POST to create a new cart.
Use the AJAX API endpoint '/cart/add.js' with the HTTP method POST to add an item to the cart. Pass the variant ID and the quantity as parameters in the request payload.
Example payload:
{
items: [
{
id: 'variant_id',
quantity: 1
}
]
}
Thank you.
Can you recommend a way/app to build a landing page for a marketing partner to drive traffic into our site? The page should have a Add to Cart button, and look similar to a product page of our site.
Hi,
I began writing API code just by trying to get cart as below. It prints the array of items in the cart. However, it is not printing "success" before printing cart array.
var url = '/cart.js';
fetch(url, {method: 'GET'})
.then(res => {
if (!res.ok) {
throw new Error('Network response was not ok');
}
return res.json();
})
.then(response => {
console.log('Success:', response);
const cart = response;
// Now you can use the cart object
})
.catch(error => {
console.error('Error:', error);
});
With these changes, your code should now log "Success:" followed by the cart data if the request is successful, and it will log any errors encountered during the fetch operation.
As 2024 wraps up, the dropshipping landscape is already shifting towards 2025's trends....
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024