Why is my order printer duplicating line items on Shopify?

Why is my order printer duplicating line items on Shopify?

GFOatsAustralia
New Member
6 0 0

Hi Forum

 

I have the following code on my Shopify Order Printer which kind of works.... 

 

 

{% for line_item in line_items %}
{% if line_item.title contains 'Subscription' %}
<p >Thank you for your subscription message here etc</p>
{% endif %}
{% endfor %}

 


So, if I have a customer with a subscription item they get this message, if not, they get the generic message which is further down the page. The problem is, if they have 4 x subscription lines, they get this message 4 times. Is there any way to have this display only once, rather than once per line_item? 

 

A variation on this code is below

 

{% for line_item in line_items %}
{% if line_item.title contains 'Subscription' %}
<p>Message 1</p>
{%- else -%}
<p>Message 2</p>
{% endif %}
{% endfor %}

 

I also tried this second method of code thinking it may be tidier, but again either Message 1 or Message 2 will display xxx about of times, depending on how many line_items are in the basket. 
Would really appreciate any support, thanks in advance

Replies 3 (3)

LukaszWiktor
Shopify Partner
315 24 124

You can break the loop after showing the first subscription message. You'll then need to list non-subscription items in a separate loop.

 

{% for line_item in line_items %}
  {% if line_item.title contains 'Subscription' %}
    <p>Thank you for your subscription message here etc</p>
    {% break %}
  {% endif %}
{% endfor %}

{% for line_item in line_items %}
  {% unless line_item.title contains 'Subscription' %}
    <p>Message 2</p>
  {% endunless %}
{% endfor %}

 

 

Another solution is to add a variable to remember if the subscription message has been already shown.

 

{% assign subscription_message_shown = false %}
{% for line_item in line_items %}
  {% if line_item.title contains 'Subscription'%}
    {% if subscription_message_shown != true %} 
      <p>Message 1</p>
    {% endif %}
    {% assign subscription_message_shown = true %}
  {% else %}
    <p>Message 2</p>
  {% endif %}
{% endfor %}

 

I'm a software engineer. I make things happen automatically.
Check out my apps Exporteo, Fulfilleo, Stockeo, and Personal Discount.
GFOatsAustralia
New Member
6 0 0

Thanks Lukaz, this is exactly what I need, thank you!

I have hit another snag, which makes me think I'm approaching this wrong...

I'm using a variation of this, to add the word "Subscription" as a prefix to the word "Order" if conditions match (just to help the packing team identify and separate subscription orders.)

I am using this code, towards the top of my invoice, and this works most of the time...

 

{% if shipping_address %}
{% for line_item in line_items %}
{% if line_item.title contains 'Subscription' %}
<p><strong>Subscription Order:{{ order_name }}</strong><br> 
{%- else -%}
<p><strong>Order No:{{ order_name }}</strong><br> 
{% break %}
{% endif %}
{% endfor %}
    {{ shipping_address.name }}<br/>
    {% if shipping_address.company %}
    {{ shipping_address.company }}<br/>{% endif %}
    {{ shipping_address.street }}<br/>
    {{ shipping_address.city }}
    {{ shipping_address.province_code }}
    {{ shipping_address.zip | upcase }}<br/>
    {{ customer.email }}<br/>
    {{ shipping_address.phone }}<br>
  </div>
{% endif %}

 

Lets say a customer has 2 items in cart as below.

1 x Subscription Product

1 x Normal Product

In this scenario my code works perfectly.

 

But, if their cart items appear the other way around (ie added to cart in this order)

1 x Normal Product

1 x Subscription Product

My code no longer works. 

Any ideas?

Thanks - Andy

 

 

 

LukaszWiktor
Shopify Partner
315 24 124

If you use {% break %} then you need two separate loops that iterate over line_items - one for subscription product and the other loop for normal products (as in my first code snippet). 

 

If you prefer to keep a single loop, then you need the additional variable to check whether a subscription product is only listed once (as in my second code snippet).

I'm a software engineer. I make things happen automatically.
Check out my apps Exporteo, Fulfilleo, Stockeo, and Personal Discount.