add a free product when the total price reaches a certain amount

add a free product when the total price reaches a certain amount

AliAhmad1
Shopify Partner
1 0 0

So, I wanted a add a free product to my cartdrawer when the price reaches upto 50 dollars so i used chatGPT for this purpose and he gave me this code and said to paste it in the onSubmit function so i did but the function is not working.

I appreciate any help, as so far I have not found anything on this. I am a developer so I shoud be ok if pointed in the right direction.

Thanks!

 

onSubmit: function(evt) {
this.submitBtn.classList.add(classes.btnLoading);

if (config.requiresTerms) {
if (this.termsCheckbox.checked) {
// continue to checkout
} else {
alert(theme.strings.cartTermsConfirmation);
this.submitBtn.classList.remove(classes.btnLoading)
evt.preventDefault();
return false;
}
}
// Check if the total price is greater than or equal to $50
// Check if the total price is greater than or equal to $50 (or 13,700 Rupees)
var cartTotal = parseInt(this.subtotal.dataset.cartSubtotal);
if (cartTotal >= 5000) {
// Define the variant ID of the free product you want to add
var freeProductVariantId = '45824736788798';

// Add the free product to the cart
jQuery.ajax({
type: 'POST',
url: '/cart/add.js',
data: {
quantity: 1,
id: freeProductVariantId,
},
dataType: 'json',
success: function() {
// Refresh the cart drawer
jQuery.ajax({
type: 'GET',
url: '/cart',
dataType: 'html',
success: function(data) {
// Update the cart drawer content
jQuery('.cart-drawer__content').html(jQuery(data).find('.cart-drawer__content').html());
}
});
}
});
}
},

Replies 0 (0)