Show information in cart if cart contains a product with a certain type

michellepaulhus
New Member
1 0 0

I am trying to add certain text to the cart page, if the customer has a product in their cart with a specific product type. What am I missing?

 

{% for item in cart.items %}
                      {% if item.product.type == 'Corporate Meals' %}
                     
        <p>Some text for corporate</p>
        {% endif %}
                    
{% endfor %}
                     
 
{% endfor %}
0 Likes
Katy
Shopify Staff
Shopify Staff
771 43 168

Hi, @michellepaulhus !

 

Katy here from Shopify. I hope all is well with you, and indeed welcome to our Community here!

 

It does appear that you have an additional {% endfor %} tag added here. (I am not sure if this is a typo ?) Otherwise however, it looks good! I do have some additional questions which may help: 

 

  • Have you added this to the cart-template.liquid file? This would be the best place to customize the cart page.
  • Also, I know this may seem like an obvious question, but have you ensured to apply the correct 'type' to the relevant products in the admin > products section of your admin? We have a guide to help apply this in bulk here.

Once you have ensured the above, I would suggest to clear your cache.  After doing so if you still don't notice any changes, if you could then share a link to your store here that would be great.


Cheers,
Katy

Please note: This account is no longer regularly monitored.
 - If you need assistance please do start a new topic (referencing an older topic/post if you find this helpful).
 - Thank you for your understanding. We hope you enjoy exploring the Community.
 - To learn more visit the Shopify Help Center or the Shopify Blog.

0 Likes
davidleitch
New Member
9 0 0

Hello,

I'm trying to adapt this so that if a product isn't in the cart, it will show a button to link to that product page.

However the button keeps appearing for each item in the cart (so if I have 3 other products, it shows the button 3 times).

I'm using the customer notes box to add a message inside the greetings card. So I'd like this to not be visible until a Greetings card is in the cart.

Example:

If "Greetings Card" isn't in the cart, the notes box won't appear. But it will show a button to go to the Greetings Cards product page.

If "Greetings Card" is in the cart, then it won't show the button, but will show the notes box.

 

Code:

{% for item in cart.items %}
                      {% if item.product.type != 'Greetings Card' %}
                     
 <input type="submit" name="view greetings cards"
class="cart__submit btn btn--small-wide"
value="/products/greetings-cards">
  
      {% endif %}
0 Likes
cessna185pilot
New Member
1 0 0

The code you want to use is:

 

 

{% for item in cart.items %}
                      {% if item.product.type == "greeting card" %}

Your content goes here

      {% endif %}
{% endfor %}

 

0 Likes
Katy
Shopify Staff
Shopify Staff
771 43 168

Hi all,

I hope you are keeping well. I would like to apologize here for the lack of response on this thread. This account is no longer regularly monitored and I have updated my signature to reflect this.

I did, however, want to circle back here to help out - and in case others may find this thread helpful also. 

To display a button that links to items not in the cart, you will need to apply some additional logic. This is to prevent additional buttons from displaying when looping through each item in the cart. For this we can use boolean logic which will set our specified variable to either true or false. Then, outside of the loop you can use this to display your preferred info (in this case a button). 

Here is an example using the Debut theme.  Note: please test this in a duplicate version of your theme before applying any changes to your live store. 

For the products here I am using ‘card’ and ‘shirt’ as examples. You would need to sub in variable names that make most sense for you for these; just make sure they are consistent with each of the steps. You'll also need to adjust the product type and hyperlinks below. 

1. Here we create and initialize the variables to true. 

 

 {% assign card_not_in_cart = true %} 
 {% assign shirt_not_in_cart = true %}

 

 

2. We then loop through the items in the cart and if there is a match then the associated variable is set to false. 

 

{% for item in cart.items %}
        {% if item.product.type == card %}          
            {% assign card_not_in_cart = false %} 
        {% endif %}
        {%if item.product.type == shirt %}          
            {% assign shirt_not_in_cart = false %}
        {% endif %}
{% endfor %}

 

 

3. The variable is then used to hide/display the buttons and cart notes below. This says if the product is not present in the cart we then show the button that links to this respective product. 

 

<div style="text-align: center;"> 
        {% if card_not_in_cart %}
       <a class="cart_submit btn btn—small-wide" href="products/card" btn>View Card</a>
        {% endif %}
      
        {% if shirt_not_in_cart %}
       <a class="cart_submit btn btn—small-wide" href="products/shirt" btn>View Card</a>
        {% endif %}
 </div>
     

 


4. Finally, to ensure that the order notes displays for greeting cards only, we will need to specify this. We can do so using an unless statement, again with our card_not_in_cart variable. You'll need to add this to the code in charge of the order notes. 

04-22-e22rl-11af7

 

Here is how this should look all in all:

05-01-0zuf2-7a1wj

I've tested this out on my own test store where I use 'Kicks' and 'Shirts' and it does the trick.  

I do apologize again for the delay on my part here, if anyone is having difficulty you are always welcome to reach out to our Community members via another thread or directly to our Support team; we can get this in front of our Theme Support who may be able to help with changes as part of your free design time

For those interested in reading more on boolean, control flow tags, and variables in Liquid we have some more info here

Katy

 

Please note: This account is no longer regularly monitored.
 - If you need assistance please do start a new topic (referencing an older topic/post if you find this helpful).
 - Thank you for your understanding. We hope you enjoy exploring the Community.
 - To learn more visit the Shopify Help Center or the Shopify Blog.

0 Likes
tiffshop
New Member
1 0 0

Hi Katy,

I am having trouble making this code work for our carts page.

{% for item in cart.items %}
{% if item.product == "Product XYZ" %}

Test Message

{% endif %}
{% endfor %}

I want the 'Test Message' to show up if the customer as Product XYZ in their cart but after adding this to our cart.liquid and other iterations of it like:

{% for item in cart.items %} {% if item.product.tags contains 'SampleProduct' %} <p>Enjoy your Product</p> {% endif %} {% endfor %}

I am not able to see any change. 

Can you advise further?

0 Likes
Katy
Shopify Staff
Shopify Staff
771 43 168

Hey @tiffshop !

Thanks for reaching out about this, I'd be happy to dig into it with you to help where I can. It is, however, a little tricky without knowing your theme. In my example, I was using the Debut theme.

From what I understand, you'd like to show a note in the cart if the customer has selected a certain product type only, much like the original poster here? Is this correct? 

I've reviewed this based on my last reply and a handy way of doing this is by following the steps you have outlined:

 

 

{% for item in cart.items %}
       	{% if item.product.title == 'XYZ' %}          
  		     <p> Your test Message </p>
 	       {% endif %}
 {% endfor %}

 

With your example, it looks like you have omitted  .title by mistake. It is also worth noting that the title is case sensitive, so this should match exactly. 

This should do the trick for you - as always do test in a duplicate version of your theme

Katy

Please note: This account is no longer regularly monitored.
 - If you need assistance please do start a new topic (referencing an older topic/post if you find this helpful).
 - Thank you for your understanding. We hope you enjoy exploring the Community.
 - To learn more visit the Shopify Help Center or the Shopify Blog.

0 Likes
Ninthony
Shopify Partner
1978 296 784

I'd suggest using the handle instead:

{% for item in cart.items %}
  {% if item.product.handle == 'your-product-handle' %}          
    <p> Your test Message </p>
  {% endif %}
 {% endfor %}

 

If my solution helped you, please like it and accept it as the solution!
If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
0 Likes