{{ "now"|date("d.m.Y") }} - date in the format: day.month.year
{{ "now"|date("d-m-Y H:i") }} - date in the format: day-month-year hours:minutes
{{ model.created_at|date_modify("+180 day")|date("m.d.Y")}}
In the basic templates, variables are written as follows:
{{ product.product_price|format_currency('USD', locale='en') }}
To replace the currency display with «dolllar», simply delete |format_currency('USD', locale='en') and move «dollar» outside the curly brackets:
{{ product.product_price }} dollar
Set the parameters for creating the desired variable:
{% set mnths = ['','January','February','March','April','May','June','July', 'August','September','October','November','December'] %}
After that, we write the variable itself in the right place:
{{ model.created_at|date("j") }} {{ mnths[model.created_at|date("n")] }} {{ model.created_at|date("Y") }}.
This method allows you to pull up the last file (image) added to the order:
{% set files = model.files|split('https://') %}
{% if files|length > 1 %}
{% set lastFile = files|last %}
{% if lastFile != '' %}
<img src="https://{{ lastFile }}" width="40" style="border: 0; background: lightgrey; max-height: 40px;"/>
{% endif %}
{% else %}
<p>There are no files attached to this order.</p>
{% endif %}
To display all files attached to an order as images, use the following code:
{% set files = model.files|split('https://') %}
{% if files|length > 1 %}
{% for file in files %}
{% if file != '' %}
<img src="https://{{ file }}" width="40" style="border: 0; background: lightgrey; max-height: 40px;" />
{% endif %}
{% endfor %}
{% else %}
<p>There are no files attached to this order.</p>
{% endif %}
If you add not only images to your orders, but also files in formats other than jpg, jpeg, png, svg, webp, you can add conditions to ignore them. Example of ignoring .pdf and .dst formats:
{% set files = model.files|split('https://') %}
{% if files|length > 1 %}
{% for file in files %}
{% if file != '' %}
{% set file_extension = file|split('.')|last|trim|lower %}
{% set is_ignored = false %}
{% for ext in ['pdf', 'dst'] %} {# 'webp' #}
{% if file_extension == ext %}
{% set is_ignored = true %}
{% endif %}
{% endfor %}
{% if not is_ignored %}
<img src="https://{{ file }}" width="200" style="border: 0; background: lightgrey; max-height: 500px;" />
{% endif %}
{% endif %}
{% endfor %}
{% else %}
<p>There are no files attached to this order.</p>
{% endif %}
Set the parameters for creating the necessary variable:
{% set total_discount = 0 %}
{% for key, product in model.products %}
{% if product.product_discount > 0 %}
{% set total_discount = product.product_discount * product.product_quantity + total_discount %}
{% endif %}
{% endfor %}
After that, we write the variable itself in the right place:
{{ product.product_price|format_currency('USD', locale='en') }}
{% set gtc = model.grand_total|round(0, 'floor') %}
{% set gtr = model.grand_total * 100 % 100 %}
{{ (gtc|format_number(style="spellout", locale="en"))|capitalize }}
{% if gtc == 1 %}
dollar
{% else %}
dollars
{% endif %}
{{ (gtr|format_number(style="spellout", locale="en")) }}
{% if gtr == 1 %}
cent
{% else %}
cents
{% endif %}
{{ model.barcode|raw }}
To add payment information to a document, you need to use a for loop. It allows you to get a list of all payments in the order and output the data of the variables you need.
An example of a list of all payments for an order with variables available for output:
{% for payment in model.payments %}
{{ payment.payment_date }} - payment date and time
{{ payment.type }} - payment type
{{ payment.status }} - payment status: paid, not_paid, canceled
{{ payment.amount }} - payment amount
{{ payment.description }} - description
{{ payment.fiscal_status }} - fiscalization status
{{ payment.fiscal_type }} - fiscalization type
{{ payment.fiscal_url }} - link to fiscal receipt
{{ payment.invoice_url }} - link to online invoice for payment
{% endfor %}
An example of displaying all records of payment by «Bank card» in an order with the status «Paid» with only the date and amount displayed:
{% for payment in model.payments %}
{% if payment.type == "Bank card" %}
{% if payment.status == "paid" %}
Payment date and time: {{ payment.payment_date }}<br>
Payment amount: {{ payment.amount|format_currency('USD', locale='en') }}
{% endif %}
{% endif %}
{% endfor %}
To add a calculation based on a formula to the print form:
Balance to be paid = Amount for products - Discount for order + Shipping cost - Amount of payments
Add this code where you want to display this amount:
{% set paid_amount = 0 %}
{% for payment in model.payments %}
{% if payment.status == "paid" %}
{% set paid_amount = paid_amount + payment.amount %}
{% endif %}
{% endfor %}
Balance to be paid = {{ (model.total_price - model.discount_amount + model.shipping_price - paid_amount)|format_currency('USD', locale='en') }}
To highlight certain products, they must differ in some parameter. Then, in the print template, you can configure the selection of only the products you need.
For example, you can highlight services by adding a field to them – a switch. And simply place it in products that are services.
Next, in the print template code, you need to add a check to the services to see if the switch is activated. If it is activated, only these products will be displayed:
{% if product.code-of-your-field and product.code-of-your-field|length %} ... {% endif %}
You can add currency formatting to the variable:
{{ product.price_sold|format_currency('USD', locale='en') }}
Or formatting to display 2 digits after the comma:
{{ product.price_sold|number_format(2, ',', '') }}
If mathematical rounding to a smaller or larger number is required:
{{ model.grand_total|round|format_currency(currency, locale='de') }}
If rounding is always required to the greater value:
{{ model.grand_total|round(0, 'floor')|format_currency(currency, locale='de') }}
If rounding is always required to the lower value:
{{ model.grand_total|round(0, 'ceil')|format_currency(currency, locale='de') }}
Add code that will cut off other properties and leave only the one whose name you specified. For example, size:
{% for product in model.products %}
{% if product.product_properties %}
{% set properties = product.product_properties|split("\n") %}
{% for property in properties %}
{% set parts = property|split(':') %}
{% if parts|length == 2 and parts[0]|trim == 'Size' %}
{% set size = parts[1]|trim %}
Size: {{ size }}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
Answer: To display the discount on an order as a percentage rather than an amount, use the code:
{% set discount_percent = (model.discount_amount / model.total_price) * 100 %}
Discount: {{ discount_percent|round }}%
You can also replace variables with product variables to display a percentage discount on products.
{% set name_parts = model.client_name|split(' ') %}
{{ name_parts[0] }} {{ name_parts[1]|slice(0, 1) }}.
{% if name_parts|length > 2 %}
{{ name_parts[2]|slice(0, 1) }}.
{% endif %}
Answer:
-
Determine how many items to display on the page (items_per_page).
-
Calculate the number of pages (pages).
-
Split the array of items into parts using slice.
-
Add a page break (page-break-before) after the first page.
-
Display the totals (Total, Discount, Grand Total, etc.) only on the last page.
{# Setting the number of products per page #}
{% set items_per_page = 5 %}
{% set products_count = model.products|length %}
{% set pages = (products_count / items_per_page)|round(0, 'ceil') %}
{# Breaking down products into pages #}
{% for page in 0..(pages - 1) %}
{% set start = page * items_per_page %}
{% set end = start + items_per_page - 1 %}
{% set products_slice = model.products|slice(start, items_per_page) %}
{% if page > 0 %}
<div style="page-break-before: always;"></div>
{% endif %}
<table class="table-container">
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
{% for product in products_slice %}
<tr>
<td>{{ product.product_name }}</td>
<td>{{ product.product_quantity }}</td>
<td>{{ product.product_price }}</td>
<td>{{ product.product_price * product.product_quantity }}</td>
</tr>
{% endfor %}
{# Add empty lines if there are fewer items_per_page products on the page #}
{% if products_slice|length < items_per_page %}
{% for i in 1..(items_per_page - products_slice|length) %}
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
{% endfor %}
{% endif %}
</tbody>
{# We show the results only on the last page. #}
{% if page == pages - 1 %}
<tbody>
<tr>
<td colspan="3" style="text-align:right">Total cost:</td>
<td style="text-align:right">{{ model.grand_total }}</td>
</tr>
</tbody>
{% endif %}
</table>
{% endfor %}