I want to show a user the total sum of all their orders. My problem: In the result the decimals are "lost" and always show as zeros.

{% set totalOrders = 0 %}
{% for order in craft.commerce.orders.customer(craft.commerce.customer).find() %}
  {% set totalOrders = totalOrders + order.totalPrice|currency(order.currency) %}
{% endfor %}
{{ totalOrders|currency(order.currency) }}

The trouble you are running into is trying to add a string value to an integer to get your total.

You have to use order.totalPrice without the currency filter to be able to add all the values together.

{% set cart = craft.commerce.cart %}
{% set totalOrders = 0 %}

{% for order in craft.commerce.orders.customer( craft.commerce.customer ).find() %}
  {% set totalOrders = totalOrders + order.totalPrice %}
{% endfor %}

{{ totalOrders|currency( cart.currency ) }}

Only apply the currency filter once all the calculations are complete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.