i have a little problem with latest version of CS-Cart. I need to insert in Document Invoice a new box with amount of Subtotal (without tax) and shipping cost.
To show this value i use this snippet:
{% set imptotale = o.display_subtotal + o.display_shipping_cost %}
{{ imptotale|number_format(2, ',', '.') }} €
Unfortunately the amount is wrong.
Example:
Subtotal: 65,10€
Shipping: 5,20€
Total: 70,30€
Value show with my snippet:
Subtotal: 65,10€
Shipping: 5,20€
Total: 70,00€
How can i show also decimal numbers?
The problems lays in the fact your are sending strings towards Twig and not floats :
input (as json)
{
'subtotal' : 65.10,
'shipping' : 5.20,
'subtotal_str' : '65,10',
'shipping_str' : '5,20',
}
twig
{% set total = subtotal + shipping %}
{{ subtotal | number_format(2, ',', '.') }}
{{ shipping | number_format(2, ',', '.') }}
{{ total | number_format(2, ',', '.') }}
{% set total = subtotal_str + shipping_str %}
{{ subtotal_str }}
{{ shipping_str }}
{{ total | number_format(2, ',', '.') }}
demo
Related
Hi I have a composite that has a QTY field and a TOTAL PRICE field and the table below would calculate and display the subtotal. It works properly when there is only one row of composite, but when I add more items, the subtotal field displays two subtotals instead of one as a whole. I want the subtotal field to display 24 instead of 4 and 20. How can twig solve this implementation? In my SUBTOTAL, I have
{% for item in data.item %}
{% set total_qty = (item.qty)|number_format(2,'.',',') %}
{% set per_price = (item.total)|number_format(2,'.',',') %}
{% set net_cost = (total_qty * per_price )|number_format(2,'.',',') %}
{{ net_cost }}
{% endfor %}
Here is the screenshot to give you better understanding
Don't output the net cost inside the for loop.
First create the sum, then display it after the loop.
Also don't use number_format before the final result.
{% set net_cost = 0 %}
{% for item in data.items %}
{% set net_cost = nest_cost + item.qty * item.total %}
{% endfor %}
{{ net_cost|number_format(2, '.', ',') }}
demo
I'm trying to print out a table with keys and values from a 2d-array in twig. The only issue is, that I'm trying to get the values and keys dynamically printed depending on the twig result I get back.
The array I'm getting back from another function can be different with every call, so my goal would be to write a function once instead of multiple times depending on the typ of data I recive.
The array I get is in {{ tableContent }}.
One example of the values are:
{
["Type"] => string(4) "2021"
["Description"] => string(11) "Stundenlohn"
["Symbol"] => string(3) "ABC"
}, {
["Type"] => string(4) "2024"
["Description"] => string(9) "Something"
["Symbol"] => string(3) "XYZ"
}
so in this case I want the table to look something like:
# | Type | Description | Symbol
--+------+-------------+--------
0 | 2021 | Stundenlohn | ABC
1 | 2024 | Something | XYZ
this would work quite well, if I always know the names of the array keys:
{% for key, u in tableContent %}
<li>{{ key }}: {{ u.Type }}</li>
{% endfor %}
And with this I'd get 0: 2021 and 1: 2024
is there a way to not use (in my case) Type but somehow the index?
With this solution I'm working on at the moment I only get the first value:
{% for array in tableContent %}
{% for id, key in array %}
{{id }} | {{key}}<br/>
{% endfor %}<br/>
{% endfor %}
I have nested content in a Twig array. I have months, each of which have days:
In my page.twig:
{% set mock = {
main_title: 'Main title',
months:
[
{
sub_title: 'Title 1',
days: [
{
monday: 'Lorum',
tuesday: 'Ipsum'
}
]
},
{
sub_title: 'Title 2',
days: [
{
monday: 'Dolorem',
tuesday: 'Neque'
}
]
}
]
}
%}
{% include "component.twig" %}
I'm trying to print each month's sub title and the day text under it:
<h2>Title 1</h2>
<h3>Lorum</h3>
<h3>Ipsum</h3>
<h2>Title 2</h2>
<h3>Dolorem</h3>
<h3>Neque</h3>
In component.twig:
{% for m in months %}
<h2>{{ m.sub_title }}</h2>
{% for d in months.days %}
<h3>Print test</h3>
{% endfor %}
{% endfor %}
The month's sub_title in <h2> is printing fine but I can't even get the days in the months to loop correctly.
It appears that the mistake is in your second loop. Instead of months.days, you need to use m.days.
Your first loop pulls the month into the variable m. As your main array months does not have an element days, but each individual month does, your inner loop currently has no content to print.
Just as a side note, I would also recommend adding escaping if this template doesn't use autoescape.
{% for m in months %}
<h2>{{m.sub_title| e}}</h2>
{% for d in m.days %}
<h3>{{ d| e }}</h3>
{% endfor %}
{% endfor %}
------edit-----
I missed on first pass that your sample array has an array "days" with a hash inside it instead of being a single level. In this case, you actually have the equivalent (in PHP anyway of an array in an array) for the days key.
This should do the trick in this case
{% for m in months %}
<h2>{{m.sub_title| e}}</h2>
{% for d in m.days[0] %}
<h3>{{ d| e }}</h3>
{% endfor %}
{% endfor %}
How is it possible in twig to format a number, without rounding off the decimals?
For example, if the user inputs
12345.35, output should be 12,345.35
12345.356, output should be 12,345.356
12345.3567, output should be 12,345.3567
I tried number_format filter but the first parameter is precision to which the decimals should be rounded to. If I use 12345.35|number_format(4, '.', ',') the result is 12,345.3500 which is not the desired output.
You should determine the number of decimals you have in the number before formatting it. Not quite obvious but I don't find another solution.
Try with this macro:
{% macro show_number(n) %}
{%- spaceless %}
{% set decimals = n | split('.')[1] | default('') | length %}
{{ n | number_format(decimals, '.', ',') }}
{% endspaceless -%}
{% endmacro %}
Runnable example here
I there,
I'm trying to find the last character for a string in twig.
What I need to do is that if the string ends with s then I only add ' Zone and if the string doesn't have an s at the end I should add 's Zone.
E.g., "Charles's Zone" should become "Charles' Zone".
Thanks a lot
{% set test_string = 'asdfs' %}
{% set test_string = test_string ~ (test_string|last == 's' ? "' " : "'s ") ~ "Zone" %}
using last
{{ [1, 2, 3, 4]|last }}
{# outputs 4 #}
{{ { a: 1, b: 2, c: 3, d: 4 }|last }}
{# outputs 4 #}
{{ '1234'|last }}
{# outputs 4 #}
I figured a workaround:
{% set title = user_name ~ "\'s zone" %}
{% set replace_value_var= "s\'s zone" %}
{% set replace_with_value_var = "s\' zone"%}
{% set MyTitle = title|replace({ (replace_value_var): replace_with_value_var }) %}