How can I link cart items count to collections count for discounts?

Hi,

Im trying to display a message when the user is at the cart that says ‘you could save more’ if the products in the cart match the targeted collection.

{% for item in cart.items %}

  {% assign product_collections = item.product.collections | map: 'title' %}

    {% if product_collections contains '5 for 20' and cart.item_count < 5 %}
      <span>you can get 5 for 20</span>
    {% endif %}

    {% if product_collections contains '4 for 10' and cart.item_count < 4 %}
      <span>you can get 4 for 10</span>
    {% endif %}

    {% if product_collections contains '3 for 10' and cart.item_count < 3 %}
      <span>you can get 3 for 10</span>
    {% endif %}

    {% if product_collections contains '3 for 35' and cart.item_count < 3 %}
      <span>you can get 3 for 35</span>
    {% endif %}

    {% if product_collections contains '3 for 20' and cart.item_count < 3 %}
      <span>you can get 3 for 20</span>
    {% endif %}

{% endfor %}

Essentially I have 5 collections that contain products and as long as the quantities match, they will be saving money. So if the user has 5 products in the cart that all have the ‘5 for 20’ collection, the total will be 20. The discounts are all working fine.

Problem is that if theres 2 products with the ‘5 for 20’ collection, and 2 products with the ‘4 for 10’ collection, the message only displays the ‘you can get 5 for 20’ since the cart total is more than 4.

Is there a way I can relate the cart items count to the collections count? Any pointers in the right direction would be great!

Hi @s_newman ,

You can refer code:

{% assign count_5 = 0 %}
{% assign count_4 = 0 %}
{% assign count_3_10 = 0 %}
{% assign count_3_35 = 0 %}
{% assign count_3_20 = 0 %}
{% for item in cart.items %}

  {% assign product_collections = item.product.collections | map: 'title' %}

    {% if product_collections contains '5 for 20' %}
      {% assign count_5 = count_5 | plus: 1 %}
    {% endif %}

    {% if product_collections contains '4 for 10' %}
      {% assign count_4 = count_4 | plus: 1 %}
    {% endif %}

    {% if product_collections contains '3 for 10' %}
      {% assign count_3_10 = count_3_10 | plus: 1 %}
    {% endif %}

    {% if product_collections contains '3 for 35' %}
      {% assign count_3_35 = count_3_35 | plus: 1 %}
    {% endif %}

    {% if product_collections contains '3 for 20' %}
      {% assign count_3_20 = count_3_20 | plus: 1 %}
    {% endif %}
{% endfor %}
{% for item in cart.items %}

  {% assign product_collections = item.product.collections | map: 'title' %}

    {% if product_collections contains '5 for 20' and count_5 < 5 %}
      you can get 5 for 20
    {% endif %}

    {% if product_collections contains '4 for 10' and count_4 < 4 %}
      you can get 4 for 10
    {% endif %}

    {% if product_collections contains '3 for 10' and count_3_10 < 3 %}
      you can get 3 for 10
    {% endif %}

    {% if product_collections contains '3 for 35' and count_3_35 < 3 %}
      you can get 3 for 35
    {% endif %}

    {% if product_collections contains '3 for 20' and count_3_20 < 3 %}
      you can get 3 for 20
    {% endif %}

{% endfor %}

You need to add a ‘for’ to count for it.

Hope it helps!