Displaying custom line item field based on cart contents

Solved
Tourist
4 0 0

Hello,

 

I have a product called "donation", only when that is in the basket I want to display a field that asks about gift aid.

 

I've successfully added the checkbox but can't get the logic to work. This is what I've got:

 

 {% for item in cart.items %}
{% if product.title == 'Donation' %}
      <p class="cart-attribute__field">
  <input type="hidden" name="attributes[Gift Aid my Donation]" value="No">
  <input type="checkbox" name="attributes[Gift Aid my Donation]" value="Yes"{% if cart.attributes["Gift Aid my Donation"] == "Yes" %} checked{% endif %}>
  <label>Gift Aid My Donation</label>
</p>
      {% endif %}
      {% endfor %}

 

Any pointers would be much appreciated!

 

Stuart

0 Likes
Highlighted
Shopify Partner
2522 25 503

Well, you're looping over items, but checking for product.title:

{% for item in cart.items %}
  {% if product.title == 'Donation' %}
 

That should be more like 

{% for item in cart.items %}
  {% if item.product.title == 'Donation' %}
 
Want to hire me to tweak a theme? Mail me at tairli@yahoo.com! My post solved your problem? This is my Paypal too :)
1 Like
Tourist
4 0 0

Thanks so much!

 

I would also like to hide the notes field when the donation product is in the cart.

 

Would you be able to assist me with that?

 

{% if section.settings.cart_notes_enable %}

 

Presumably I need some sort of and statement here to hide the notes field.

 

Thanks,

Stuart

0 Likes
Shopify Partner
2522 25 503

Then do like this: 

{% for item in cart.items %}
  {% if item.product.title == 'Donation' %}
      {% assign no_note = true %}

...

{% if section.settings.cart_notes_enable and no_note == false %}

 

 

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com! My post solved your problem? This is my Paypal too :)
0 Likes
Tourist
4 0 0

Thanks, that seems to hide it completely regardless of what's in the basket....

0 Likes

Success.

Shopify Partner
2522 25 503

Right, undefined (null) is not false, strictly speaking. 

So either 

{% assign no_note = false %}
{% for item in cart.items %}
  {% if item.product.title == 'Donation' %}
      {% assign no_note = true %}

...

{% if section.settings.cart_notes_enable and no_note == false %}

or  

{% for item in cart.items %}
  {% if item.product.title == 'Donation' %}
      {% assign no_note = true %}

...

{% if section.settings.cart_notes_enable and no_note == null %}

 

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com! My post solved your problem? This is my Paypal too :)
1 Like