Search code examples
shopifyliquidinvoice

How scan an Order with Shopify Liquid - for customised Invoice


We are producing Invoices with Shopify's 'Order Printer' app. and want to customise the Invoice. For instance, if they have bought a 'book' - we want it to say "Enjoy your book" and if a 'CD' - "Enjoy the music".

I've found I can test the first item they purchased with 'limit:1' :

{% for line_item in unfulfilled_line_items limit:1 %} 
    productType: {{ line_item.product.type }} -   prodtype:{{product.type}} <br/>
    {% if line_item.product.type contains "cd" %}
    its a CD <br/>
    {% else %}
    it's not a CD?)<br/>
{% endif %}
{% endfor %}

But I would really like to scan the whole of the product.type array to determine how many of each product type there are - and output either/both messages - with plural 's' as appropriate.

Any ideas?


Solution

  • You're on the right track instead of limiting though you basically want to count.

    {% assign cd_count = 0 %}
    {% assign book_count = 0 %}
    {% for line_item in unfulfilled_line_items %}
        {% if line_item.product.type == "cd" %}
            {% assign cd_count = cd_count | plus: 1%}
        {% endif %}
        {% if line_item.product.type == "book" %}
            {% assign book_count = book_count | plus: 1 %}
        {% endif %}
    {% endfor %}
    cd count: {{ cd_count }}
    book count: {{ book_count}}
    

    Now that you have a count you should be able to just do an if statement of the count numbers.