如何使用 Shopify Liquid 扫描订单 - 用于定制发票

How scan an Order with Shopify Liquid - for customised Invoice

我们正在使用 Shopify 的 'Order Printer' 应用生成发票。 并希望自定义发票。 例如,如果他们购买了 'book' - 我们希望它显示 "Enjoy your book" 如果 'CD' - "Enjoy the music".

我发现我可以测试他们用 '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 %}

但我真的很想扫描整个 product.type 数组以确定每种产品类型有多少 - 并输出 either/both 消息 - 复数 's' 作为合适。

有什么想法吗?

虽然你基本上想数数,但你走在正确的轨道上而不是限制。

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

现在你有了一个计数,你应该可以对计数做一个 if 语句。

感谢@trowse - 解决了零问题,它们是由于 OrderPrinter 缓存问题和限制造成的。以防万一有人需要它。这是我们的解决方案:

<!--  count how many of each product type we are/have????? fullfilling -->
        {% assign count_cd = 0 %}
        {% assign count_bk = 0 %}
        {% for line_item in unfulfilled_line_items %}
            {% if line_item.product.type contains "cd" %}
                {% assign count_cd = count_cd | plus:1 %} 
            {% endif %}
            {% if line_item.product.type  contains "Book" %}
                {% assign count_bk = count_bk | plus:1 %} 
            {% endif %}
        {% endfor %}
<!--- end of counting -->

<!-- Enjoy.. message -->
    {% if {{count_cd > 0 %}
        Enjoy the music 
        {% if {{count_bk > 0 %}
            and the {{ count_bk | pluralize: 'book', 'books' }}<br/>
        {% endif %}
    {% else %}
        {% if {{count_bk > 0 %}
        Enjoy the {{ count_bk | pluralize: 'book', 'books' }}<br/>
        {% endif %}
    {% endif %}