Personalized checkout and custom promotions with Shopify Scripts
Hi,
I have 2 items where I want to display a message in cart depending on the following scenarios:
1. User has a lite only in cart
You have a Lite in cart.
2. User has a Plus only in cart.
You have a Plus in cart
3. User has both in cart.
You have both lite and plus in cart.
The current script I wrote is
{%- for item in cart.items -%}
{% if item.product.type == "Lite" and item.product.type != "Plus" %}
You have a Lite in cart.
{% elsif item.product.type == "Plus" and item.product.type != "Lite" %}
You have a Plus in cart.
{% elsif item.product.type == "Plus" and item.product.type == "Lite" %}
You have both lite and plus in cart.
{% endif %}
{% endfor %}
Can someone point me in the right direction please?
Solved! Go to the solution
This is an accepted solution.
You are looping over each item in the cart so this means you're looking at just one of them at a time.
A line like this will never work as i assume an item can not be both lite and plus.
{% elsif item.product.type == "Plus" and item.product.type == "Lite" %}
If you want to keep that loop what's better is to set some variables first, and mark them as true or not as you asses the entire cart.
Try something like the below. Now this code could be slightly wrong as I've just typed it here quickly without actually testing. It should give you enough inspiration to get it working though.
{%- assign hasLite = false -%}
{%- assign hasPlus = false -%}
{%- for item in cart.items -%}
{%- if item.product.type == "Lite" -%}
{%- assign hasLite = true -%}
{%- continue -%}
{% elsif item.product.type == "Plus" %}
{%- assign hasPlus = true -%}
{%- continue -%}
{% endif %}
{% endfor %}
{%- if hasLite and hasPlus -%}
both
{%- elsif hasLite -%}
just lite
{%- elsif hasPlus -%}
just plus
{%- endif -%}
This is an accepted solution.
You are looping over each item in the cart so this means you're looking at just one of them at a time.
A line like this will never work as i assume an item can not be both lite and plus.
{% elsif item.product.type == "Plus" and item.product.type == "Lite" %}
If you want to keep that loop what's better is to set some variables first, and mark them as true or not as you asses the entire cart.
Try something like the below. Now this code could be slightly wrong as I've just typed it here quickly without actually testing. It should give you enough inspiration to get it working though.
{%- assign hasLite = false -%}
{%- assign hasPlus = false -%}
{%- for item in cart.items -%}
{%- if item.product.type == "Lite" -%}
{%- assign hasLite = true -%}
{%- continue -%}
{% elsif item.product.type == "Plus" %}
{%- assign hasPlus = true -%}
{%- continue -%}
{% endif %}
{% endfor %}
{%- if hasLite and hasPlus -%}
both
{%- elsif hasLite -%}
just lite
{%- elsif hasPlus -%}
just plus
{%- endif -%}
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024