Twig Math: Subtract Cart Total from Minimum order amount for Free shipping

Hi!

I thought this would be easy with Twig, but I just cannot get it working. I need to subtract the Cart total from 40,00 €, which is the minimum order amount for free shipping.

I have the current cart amount: {{ woocommerce.cart_total_raw }}. (I tried the other cart total versions as well). This outputs 8,78 €, with a comma and with a space.

I have the Minimum order amount set dynamically: {{woocommerce.free_shipping_min}}, but this can be hard-coded to 40,00 € if that makes it simpler.

I have a hard-coded Twig calculation that works:

{% set currency = ' €' %}
{% set min_clean = '40.00' %}
{% set cart_clean = '8.78' %}
{% set remaining = max(min_clean - cart_clean, 0) %}
{{ remaining | number_format(2, ',', '.') ~ currency }}

But, I simply cannot get the dynamic one to work, that would show the remaining amount left until free shipping. (I tried with the “Advanced” extension on as well).

Can you please help me with this?
Thank you!

Try with this replace and striptags. Really we need way to get the raw number which you would assume is what raw does, but there is html that outputs in the woocommerce function we are calling. I’ll probably add an argument to the dynamic content to get a float value. Let us know if this helps. Have a great weekend.

{% set total = woocommerce.cart_total_raw | striptags | replace({".": "", ",": ".", "€": ""}) %}

1 Like

Thank you @charlie!

I am managing to get calculations now, but that’s quite complex, and I still need raw cart numbers in conditions, to be able to manage various scenarios that are pretty common for Web shops. For example, right now, I am managing this by complex php:

  • If Cart total is less than the shipping threshold with taxes, display the remaining amount.

  • If Cart total is equal or higher than the shipping threshold, display the message that the customer is eligible for free shipping. (Only local, not if shipping abroad).

  • If a product with an attribute “Frozen” has been added to the cart, remove the free shipping count because such a product cannot be shipped…

I am not much of a programmer, but I’m kind of getting it with the help of AI. Still, such things should be native to Pro. That argument in Dynamic content you are mentioning would have a massive impact.

Thank you!

Hi,
i did code smothing for a site:
ACF repeater field, some php and java: I can enter a min and max value and the text and a icon:
if under 99€ then show text 1 with icon 1. when between 100 and 150€ show text 2 and icon2…
i then show this xx€ left until text/icon in the mini cart with a shortcode
free_until

It does change the amount in relatime and it does work with multilanguage sites.

If interested i can share the code for it here.
bye

2 Likes

@deranaloge thanks! I do have the complete functionality via php. Everything is working. But I think that such things should be available in Pro for less tech-savvy users, utilizing Dynamic content and conditions.

1 Like

Thank you for your feedback @Misho. That is noted.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.