Update jinja variable value in custom print format

How to update total_price which set in custom print format. I can’t update the variable value. How to update this variable value?

{% set total_price = 0 %}
{% for item in doc.items %}
    {% set total_price = total_price + (item.price_list_rate * item.qty) %}
{% endfor %}

Hi,

Please try to change the code as follows:

{% for item in doc.items %}
    {% set total_price +=  item.price_list_rate %}
{% endfor %}

Thanks,

Divyesh Mangroliya

Thanks for your response. I try it. But doesn’t work again…

Hi @Samsul,

Please try it.

{{ doc.items | sum(attribute='price_list_rate') }}

Then reload and check it.

Reference:

Thank You!

Thank you brother for your reply.
Actually, I need two fields value calculation. And also need update the set variable every loop.
Note: I already update my question code.

Thanks

Please try it.

{% set total = 0 %}
{% for item in doc.items %}
    {% set line_total = item.price_list_rate * item.qty %}
    {% set total = total + line_total %}
{% endfor %}
{{ total }}

I already try your code like this:

{% set total_price = 0 %}
 {% for item in doc.items %}
      {% set line_total = item.price_list_rate * item.qty %}
      {% set total_price = total_price + line_total %}
{% endfor %}

But It doesn’t work again. Show value 0.
image

Hi @Samsul,

Please apply it.

{% set total = [0] %}
{% for item in doc.items %}
    {% set line_total = item.price_list_rate * item.qty %}
    {% if total.append(total.pop() + line_total) %}{% endif %}
{% endfor %}

Total: {{ total.pop() }}

Then reload and check it.

Thank You!

2 Likes

It works perfectly.
Thank you so much