Hello I got this logic for adding products to cart, but when adding them I want to change the price of the products because I’m trying to create like a volume bundle offer. URL: Glow Curtain: 400 LED Lights for a Magical Ambiance – InteriorGlows. I tried doing cart_level_discount_applications: [
{
type: ‘percentage’,
amount: 1349, // This is the discount amount (for example, 10% of the product price)
description: ‘10PERCENT Applied’, // Description of the discount
}
] in the body, but that did not apply an extra discount. Does anyone know how I can do this?
function addProductsToCart() {
console.log("triggered");
const productsToAdd = [];
// Select the currently selected row
const selectedRow = document.querySelector('.volume-bundle__row.selected');
if (!selectedRow) {
console.error("No selected row found.");
return;
}
selectedRow.querySelectorAll('[data-variant-picker]').forEach(pickerContainer => {
const pickers = pickerContainer.querySelectorAll('.variant-selector');
const selectedValues = Array.from(pickers).map(picker => picker.value);
console.log("Selected Values:", selectedValues);
const variantId = getVariantIdFromSelectedValues(selectedValues, product);
if (variantId) {
productsToAdd.push({ id: variantId, quantity: 1 });
}
});
if (productsToAdd.length) {
console.log("Try to add");
//setTimeout(() => {
fetch('/cart/add.js', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
items: productsToAdd,
/*cart_level_discount_applications: [
{
type: 'percentage',
amount: 1349, // This is the discount amount (for example, 10% of the product price)
description: '10PERCENT Applied', // Description of the discount
}
]*/
}),
})
.then(response => {
if (!response.ok) {
throw new Error("Failed to add products to cart");
}
return response.json();
})
.then(data => {
console.log("Products added to cart:", data);
})
.catch(error => {
console.error("Error:", error);
});
//}, 100); // Add a short delay (e.g., 100ms)
} else {
console.error("No valid products to add.");
}
}

