Questions - Answers (FAQ) Creating Documents for Orders

This FAQ addresses the most common questions that our users encounter when editing code for documents.
Written by Владислав Пономарь
Updated 1 month ago
Important! If you have questions about how to interact with variables when editing documents, you can find answers in the Twig documentation. Available tags, filters, and functions are described in our manual.
1. How do I add today's date to the display?
{{ "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

2. How to add the order date or delivery/shipping date + n-number of days to a variable?
{{ model.created_at|date_modify("+180 day")|date("m.d.Y")}}

3. How to change the currency display of a variable from USD to dollar?

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

4. How can I change the date format of an order from 2022-12-22 12:12:12 to the more familiar December 22, 2022?

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") }}.

5. How to change the delivery/shipping date format from 2022-12-22 to the more familiar 22.12.2022

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 %}

7. How to add to display the total value of discounts on products?

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') }}

8. How to display the total order cost in words?
{% 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 %}

9. What variable should be added to display the order barcode?
{{ model.barcode|raw }}

10. How do I get payment info for an order?

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 %}

11. How to display the remaining amount to be paid?

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') }}

12. How to print a document containing only certain items from an order?

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 %}

13. How to display cents for variable prices or amounts?

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, ',', '') }}

14. How to add price rounding?

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') }}

15. How to display only the necessary product property?

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 %}

16. How do you convert a discount amount into a percentage value?

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.

17. How do I display the customer's name in the format of last name + initials?
{% 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 %}

18. How do I split a table with products into several pages?

Answer:

  1. Determine how many items to display on the page (items_per_page).

  2. Calculate the number of pages (pages).

  3. Split the array of items into parts using slice.

  4. Add a page break (page-break-before) after the first page.

  5. 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>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</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 %}

Tags: document templates, document code, variables, code examples
Did this answer your question?