Hi everyone,
I’m trying to update the cart price of an item based on a user’s input in an app block, and I’m writing this in Liquid. Here’s the JavaScript code I’m using:
// Assuming there is an input field where users enter their desired price
let userEnteredPrice = parseFloat(document.getElementById(‘userPriceInput’).value);
// Form data with user’s price input
let formData = {
‘items’: [{
‘id’: selectedVariantId,
‘quantity’: 1,
‘price’: userEnteredPrice
}]
};
fetch(window.Shopify.routes.root + ‘cart/add.js’, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’
},
body: JSON.stringify(formData)
})
.then(response => response.json())
.then(data => {
console.log(‘Item added to cart:’, data);
})
.catch(error => {
console.error(‘Error:’, error);
});
However, I’m not sure if directly updating the price
like this is the correct approach. Is there a better way to update the price based on user input in Shopify?