# How to calculate total amount based on doc items using jinja template

Hello All,

I want to calculate the total amount based on doc items of sales order I referred base erpnext code but can’t find any reference related to calculating the total.

I want to calculate total amount and show it in shopping cart Sales order.

Below is my code:

{%- set total_amount = 0.0 -%}
{%- for item in doc.items -%}
{{ total_amount + (item.price_list_rate * item.qty) }}
{% endfor %}

In my system, jinja version is: Jinja2 2.8

Its considering your variable as string.

Try someting total_amount += (item.price_list_rate*item.qty)

If above dosent work you can always try to pars result as float

Sry, I forgot to mention it in my question.
I already try that but it was given the below error:

/frappe-bench/apps/erpnext/erpnext/./templates/includes/order/order_taxes.html", line 50, in template
{{ total_amount += (item.price_list_rate * item.qty) }}
TemplateSyntaxError: unexpected ‘=’

Also, Tryied below code but its also not give expected result:

{%- set total_amount = 0.0 -%}
{%- for item in doc.items -%}
{% set total_amount = total_amount + (item.price_list_rate * item.qty) %}
{% endfor %}

Is there another way to do it?

what’s the result of this code?

the result only shows the 0.0 or last item total not all items total.
that we initialize in the variable.
Means In the code below expression not working.

Hello All
Any other way for it?

``````    {% set total_amount = [] %}
{%- for item in doc.items -%}
{% if total_amount.append((item.price_list_rate * item.qty)) %}{% endif %}
{% endfor %}
{{_("{0}").format(total_amount | sum)}}``````
1 Like

Or

``````{% set vars = {'total_amount': 0} %}
{% for item in doc.items %}
{% if vars.update({'total_amount': vars.total_amount + item.amount }) %}{% endif %}
{% endfor %}
{{ vars.total_amount }}``````
1 Like

This code working very well, thank you

See below :