Different message on cart page dependent on if item put in is a subscription or not

Different message on cart page dependent on if item put in is a subscription or not

matthew44
Shopify Partner
9 0 1

Hi,

 

I am trying to add a shipping message on the Shopify Cart Page. But would like to show a different message if a item in the cart is for a subscription. But am unsure on how to do this, as I can not seem to find a attribute for if a item is on subscription or not.

 

Below is my current code, I would be looking to do something like {% if subscription _value_ > 0 } for example. 

 

<div class="shipping_container">

{% assign shipping_value = 4000 %}

{% assign cart_total = cart.total_price %}

{% assign shipping_value_left = shipping_value | minus: cart_total %}

</div>

<p class="shipping-savings-message">

{% if shipping_value_left > 0 %}

You are {{ shipping_value_left | money }} away from FREE shipping! (UK mainland only) <br>

 

  {% else %}

  You've got free shipping! (UK mainland only)

  {% endif %}

</p>

 

Thanks 

Replies 6 (6)

J-ROM
Shopify Staff (Retired)
66 6 8

Hey Matthew44,

 

Thank you for your patience while we got back to you.

According to the Liquid objects reference doc, you can figure out if the cart contains any subscription products by inspecting cart.items.selling_plan_application. This object will only be set for subscription products, and will give you access to a couple of prices that will let you calculate the total subscription value of the cart.

Hope this helps.
J-ROM

To learn more visit the Shopify Help Center or the Community Blog.

matthew44
Shopify Partner
9 0 1

Hi J-Rom,

 

That is very helpfully thanks. I have changed {% if shipping_value_left > 0 %} to {% if shipping_value_left > 0 or selling_plan_allocation.price < 19 %} however it dose not seem to be working, Is there anything I am missing?  Full code below.

 

Thanks 

 

 

<div class="shipping_container">
{% assign shipping_value = 4000 %}
{% assign cart_total = cart.total_price %}
{% assign shipping_value_left = shipping_value | minus: cart_total %}
</div>
<div class="shipping-savings-message">
{% if shipping_value_left > 0 or selling_plan_allocation.price < 19 %}
<p class="shipping-2"> You are {{ shipping_value_left | money }} away from FREE shipping* <br>
Subscription customers get FREE shipping* on all orders over £20 <br> </p>
<p class="shipping-small-font">
*UK mainland only
</p>
<p>
{% else %}
You've got free shipping! (UK mainland only)
{% endif %}
</p>
</div>

J-ROM
Shopify Staff (Retired)
66 6 8

How are you retrieving selling_plan_allocation? In the code provided it's used without being declared, but this isn't a global variable like cart. It's set on every line_item object found in the cart.items array. You're probably looking at an iteration tag to calculate the total price of subscription products:

 

{% assign subscription_total = 0 %}

{% for line_item in cart.items %}
   {% if line_item.selling_plan_allocation %}
      {% subscription_total += line_item.selling_plan_allocation.price %}
   {% endif %}
{% endfor %} 


Hope this helps.
J-ROM

To learn more visit the Shopify Help Center or the Community Blog.

matthew44
Shopify Partner
9 0 1

Ok thanks that makes sense, didn't realize it needed declared.

 

I have added the code you wrote but it is saying unknown tag error (I have attached a screenshot) not sure why as it looks like it should be a known tag from line 22?

 

Thanks for the help, sorry If I am missing something simple.

 

matthew44
Shopify Partner
9 0 1

sub.PNG

J-ROM
Shopify Staff (Retired)
66 6 8

My bad, it's not possible to change the value of a variable once it's been assigned, so this approach won't work.

I would recommend exploring the Liquid docs to explore what operations you could do on that array of line_items.

Hope this helps.
J-ROM

To learn more visit the Shopify Help Center or the Community Blog.