Only show discount when there is discount

I’m using a invoice template with a discount tab. Is there a way to show only the discount column when i have given a discount?

I know i have to use something like:
{% if has_discount %}

{% if line.discount != null %}{{ line.discount }} %{% endif %}{% endif %}

But i don’t can get it to work with my template. Someone who can help me?
My invoice template can be found here:
https://jsfiddle.net/#&togetherjs=9KjR8RPfcS

You’re on the right track, but you don’t want to suppress the discount cell of individual items if there’s no item discount. That happens automatically (that is, if there’s no discount for an item, that cell will be left blank for that row).

Get rid of the {% if line.discount != null %} clause. All you need to do is to suppress the entire Discount column if there are no discounts on the entire invoice, which is what the has_discount boolean indicates.

Also, make sure you put the if clause around everything that affects the number of columns in the table, including the header row and the tax-and-total rows on the bottom. (For the latter, you’ll have to change the colspan attribute from 5 to 4 if there’s no discount column in the table. Use ifelseendif surrounding the attribute.) If you don’t do this, the columns won’t line up between the item-list part of the table and the tax-and-totals part of the table.

I can’t see your template, but here is the relevant part of a template that will accomplish what you want (starting at line 51 of the default template template, but you’ll still need to take care of the tax-and-totals part):

<table style="width: 100%">
  <tr>
    <th style="border: 1px solid #000; padding: 5px 10px">Item</th>
    <th style="border: 1px solid #000; padding: 5px 10px">Description</th>
    <th style="width: 40px; border: 1px solid #000; padding: 5px 10px; text-align: center">Qty</th>
    <th style="width: 80px; border: 1px solid #000; padding: 5px 10px; text-align: center">Unit price</th>
    {% if has_discount %}
      <th style="width: 60px; border: 1px solid #000; padding: 5px 10px; text-align: center">Discount</th>
    {% endif %}
    <th style="width: 100px; border: 1px solid #000; padding: 5px 10px; text-align: center">Amount</th>
  </tr>
  {% for line in lines %}
  <tr>
    <td style="border: 1px solid #000; padding: 5px 10px; border-bottom: none; border-top: none; vertical-align: top">{{ line.item.name }}</td>
    <td style="border: 1px solid #000; padding: 5px 10px; border-bottom: none; border-top: none">{{ line.description | newline_to_br }}</td>
    <td style="border: 1px solid #000; padding: 5px 10px; border-bottom: none; border-top: none; text-align: center; vertical-align: top">{{ line.qty }}</td>
    <td style="border: 1px solid #000; padding: 5px 10px; border-bottom: none; border-top: none; text-align: right; vertical-align: top">{{ line.unit_price | money_without_currency }}</td>
    {% if has_discount %}
      <td style="border: 1px solid #000; padding: 5px 10px; border-bottom: none; border-top: none; text-align: center; vertical-align: top">{{ line.discount }}</td>
    {% endif %}    
    <td style="border: 1px solid #000; padding: 5px 10px; text-align: right; border-bottom: none; border-top: none; vertical-align: top; white-space: nowrap">{{ line.total | money_without_currency }}</td>
  </tr>
  {% endfor %}
  <tr>
    <td style="border: 1px solid #000; padding: 5px 10px; border-top: none">&nbsp;</td>
    <td style="border: 1px solid #000; padding: 5px 10px; border-top: none">&nbsp;</td>
    <td style="border: 1px solid #000; padding: 5px 10px; border-top: none">&nbsp;</td>
    <td style="border: 1px solid #000; padding: 5px 10px; border-top: none">&nbsp;</td>
    {% if has_discount %}
      <td style="border: 1px solid #000; padding: 5px 10px; border-top: none">&nbsp;</td>
    {% endif %}
    <td style="border: 1px solid #000; padding: 5px 10px; border-top: none">&nbsp;</td>
  </tr>


The result looks something like this:


1 Like

Thanks for your reponse. I managed to get it to work!Awesome