Show the remaining amount before free delivery (studio theme)

Show the remaining amount before free delivery (studio theme)

UTOPIA-PARIS
Explorer
57 1 10

Hey !

 

how can i show the remaining amount before free delivery in the cart header product  view after add to basket the product like this :

 

 

WhatsApp Image 2024-10-28 at 16.51.42.jpeg

 

website: utopia-paris.fr

theme : Studio

Reply 1 (1)

DaisyVo
Shopify Partner
4447 496 590

Hi @UTOPIA-PARIS 

To display the remaining amount needed for free delivery in the cart header after a product is added, you can use JavaScript to calculate and update the message dynamically. Here's how:

1. Set the free shipping threshold in your code:

const freeShippingThreshold = 50; // Replace with your free delivery threshold.

2. Calculate the remaining amount:

function updateFreeShippingMessage(cartTotal) {

    const remaining = freeShippingThreshold - cartTotal;

    const message = remaining > 0

        ? `Add $${remaining.toFixed(2)} more for free delivery!`

        : 'You qualify for free delivery!';

    document.querySelector('#free-shipping-message').innerText = message;

}

3. Update the cart header when products are added: Use Shopify's AJAX cart API to retrieve the cart total and call the update function.

4. Example Integration:

fetch('/cart.js')

    .then(response => response.json())

    .then(cart => {

        updateFreeShippingMessage(cart.total_price / 100); // Convert cents to dollars.

    });

5. Add the message container to your theme:

<div id="free-shipping-message"></div>

This will dynamically update the message in the cart header when products are added.




If you have other questions, I am willing to answer them more.

 

Best regards,

Daisy

Please let us know if our reply is helpful by giving it a Like or marking it as a Solution!

Avada SEO & Image Optimizer - The #1 SEO solution