Print format sum total for custom field

hello
i added cost for print field and i want to sum the total of each row and print it out
i am using this code but the total is 0 any help in this
{% set grand_total = 0 %}

{% for row in doc.items %}
{% set row_total = (row.qty|float) * (row.cost_for_print|float) %}
{% set grand_total = grand_total + row_total %}
{% endfor %}

Merchandise Total: {{ grand_total }}

Dear @yara
Jinja is somehow not supporting value outside the loop. I dont know is this the correct way … but it solves our problem.


 {% set grand_total =namespace(total=0) %}

{% for row in doc.items %}
{% set row_total = (row.qty|float) * (row.cost_for_print|float) %}

{% set grand_total.total = grand_total.total + row_total %}
{% endfor %}

Merchandise Total: {{ grand_total.total }}

thank you for you response
I solve it in another way i added a field and sum the total using client script then print it :pray: