Display an Accept Terms checkbox at checkout but only for certain product types

Highlighted
Excursionist
28 0 18

I have added a terms checkbox to my cart page that shoppers must check in order to proceed to checkout.  It works fine. The problem is that it is displayed by default regardless of the products in the cart.

How can I insert an "if" conditional statement that will only show the terms checkbox if the cart contains one or more products of a certain ProductType?  In other words, if the cart contains a ProductType of XYZ then [my current code].  I just don't know how to make it ProductType specific for the contents of the cart.

There are some great minds here!  Thanks.

0 Likes
Highlighted
Shopify Staff (Retired)
Shopify Staff (Retired)
288 0 222

Hey Jamie,

Sounds like you're looking for something like:

{{if line_item.type = 'type, typeb, typec'}}

The cart can't see product Liquid snippets, but it can recognize line_item information and cart information. You could also use line_item.vendor if that works better.

Hope that helps!

0 Likes
Highlighted
Shopify Expert
10006 116 1819

The cart can't see product Liquid snippets

This makes no sense. The cart can pull in snippets, and can access the product so not sure what you're trying to say here.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Highlighted
Excursionist
28 0 18

Well that got me a lot closer.

I now have this:

{% for item in cart.items %}
     {% if item.product.type == 'typeA' %}
           {% if forloop.first %}
                   [my checkbox code is here]
           {% endif %}
      {% endif %}
 {% endfor %}

 

That is working as long as all the items in the cart are product.type of 'typeA'.  The looping statement prevents it from showing multiple times for mutilple 'typeA' items in the cart.  But as soon as I add a product.type 'typeB' to the cart it goes away.  I want to show it if one or more of the items in the cart is of 'typeA' even if there are other types of items in the cart.  If the cart has no 'typeA' items I do not want it to show.

How can I get it to work if any item in the cart is a 'typeA' item and not work unless one or more of the items in the cart is a 'typeA' item?

0 Likes
Highlighted
Astronaut
1890 1 443

Try this:

{% assign product_types = "typeA | typeB" %}
{% assign product_types = product_types | split: "|" | strip %}
{% for item in cart.items %}
{% if product_types contains item.product.type %}
[my checkbox code is here]
{% break %}
{% endif %}
{% endfor %}

Change the first line to include a list of the product types that you want to check for, each separated by "|".

 

If you're curious as to why your previous code doesn't work, it is only checking the first line_item.  When a new line_item is added to cart, it takes the first spot and pushes the other line_items down.  Therefore your previous code is only checking the product type of the most recently added line_item.

0 Likes
Highlighted
Excursionist
28 0 18

I finally figured it out myself.  I am not a coder so it took me a few hours and lots of trial and error.

This works like a charm in my React Theme:

{% for item in cart.items %}
        {% assign no_typeA_in_cart = true %}
            {% if item.product.type == 'typeA' %}
                   {% assign no_typeA_in_cart = false %}
               {% endif %}
            {% if no_typeA_in_cart %}    
              <!-- don't display the message  -->
               {% else %}
                   {% if forloop.first %} 
                    [my checkbox code is here]
                 {% endif %}
            {% endif %}
        {% endfor %}

This does nothing unless there is a product with a product.type property of 'typeA' in the cart.  Once you add a typeA product to the cart the message appears.  The message appears if the cart contains any mix of products as long as at least one product of typeA is in the cart.  You can add more typeA products to the cart but the loop statement prevents the checkbox message from being repeated for each item.  If you remove all typeA products the message disappears.

I welcome any suggestions for a cleaner or easier way to do this.

0 Likes
Highlighted
Excursionist
28 0 18

Wow, Alex, now I'm going to have to figure that out.  I was posting my post above and missed your reply till afterwards.  I have no idea what "| split: "|" | strip" means or does. But it does look like that way requires less logic. I've got a lot to learn.

Thanks.

0 Likes
Highlighted
New Member
1 0 0

Hi, this seems to work, but what about the code that checks the checkbox... how do that look and where do you add it.. just now I have a script in my theme.liquid and your code in the cart.liquid and it works = it only show up if products from a vendor I have added.. and the checkbox works... the issue is that if a product from the vendor is not in the cart and the checkbox do not show, the user still gets the popup saying she/he did not check  the box... as to the box not being shown ?

Any solution for that ?

 

Thanks

Jacob

0 Likes