Hello everyone,
I got a question regarding adding a product to the cart using the Ajax API.
The product this is about would be a custom bundle product whose price should change dynamically based on which options the user has selected. Once the user clicks the “Add to cart” button the bundle product should be added to the cart with the dynamically calculated price.
While it is not too difficult to use the API to add a product to the cart, it seems that it is not possible to also submit a custom price via the Ajax API. If I got it right, this would only be possible using the Admin API. But then again it always seemed to me as if the Admin API would not really be about working within the actual Theme but rather for building more complex applications outside of the theme.
What I would like to do is to implement a price-update functionality simply within a given .js or .liquid file in the Theme’s code once the product is being added to the cart. Is this even possible?
A workaround might be to use Shopify Scripts to reduce the bundles price based on the selected options. But I’d prefer a more direct solution by sending the reduced product’s price to the user’s cart immediately.
Here’s how it would work ideally for my usecase (but obviously doesn’t):
const formData = {
'items': [{
id: 39641211961437,
quantity: 1,
price: 2200 // dynamically calculated price based on bundle options
}]
};
fetch('/cart/add.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formData)
});
Any help or clarification would be greatly appreciated.
Kind regards
Yannic