<p>Try something like this:</p>

<pre><code>{% set currentDiscount = craft.commerce.getDiscountByCode(cart.couponCode) %}

{% if currentDiscount %}

{% set cartProductIds = [] %}

{% for item in cart.lineItems %}
  {% set cartProductIds = cartProductIds|merge([item.purchasable.product.id]) %}
{% endfor %}

{% for product in currentDiscount.products %}
  {% if not product.id in cartProductIds %}{{ product.title }} not in cart.{% endif %}
{% endfor %}

{% endif %}
</code></pre>

CC BY-SA 3.0
2016-09-27T17:39:56.460Z
Thanks @lukeholder. But it throws an error Variable "cartProductIds" does not exist ?
Terry Upton
CC BY-SA 3.0
2016-09-28T03:05:35.577Z
That will be a scope error - with the mergeg filter the variable must already exist I believe - I've adjusted the code above to create the empty array, then new Ids are merged in to it as before.
Jeremy Daalder
CC BY-SA 3.0
2016-09-28T08:14:34.290Z
Thanks @JeremyDaalder - this no longer throws up a Craft error but I still can't get it to give me any feedback?

Each time I try to check if something is set (for example) {{currentDiscount}} or {{cartProductIds}} to check if they are set and have values, then I get an error like; Array to string conversion.

I'm a bit stuck with this one.
Terry Upton
CC BY-SA 3.0
2016-09-28T08:21:13.897Z
Just realised I need to do a '{{ dump(currentDiscount) }}' as it is an array.
Terry Upton
CC BY-SA 3.0
2016-09-27T16:16:38.960Z
2016-09-28T03:04:39.703Z
2016-09-28T03:04:39.703Z
Luke Holder
<p>So some persistence I have this working as follows:</p>

<pre><code>{% set currentDiscount = craft.commerce.getDiscountByCode(cart.couponCode) %}

{% if currentDiscount %}
    {% set cartProductIds = [] %}
    {% set codeError = null %}

    {% for item in cart.lineItems %}
        {% set cartProductIds = cartProductIds|merge([item.purchasable.product.id]) %}
    {% endfor %}

    {% for product in currentDiscount.products %}
        {% if not cartProductIds in product %}
            {% set codeError = 'true' %}
        {% endif %}
    {% endfor %}

    {% if codeError == 'true' %}
        &lt;br /&gt;
        {% include '_includes/formError' with { error : 'Sorry there are no products in your cart that are eligiable for the code ' ~ cart.couponCode } %}
    {% endif %}
{% endif %}
</code></pre>

<p>Sure the code can be cleaned up a little. So feel free to refine if anyone wishes to make it better.</p>

CC BY-SA 3.0
2016-09-28T08:37:31.200Z
2016-09-28T08:37:31.200Z
Terry Upton
<p>I have now updated this code for Craft Commerce 2:</p>
<pre><code>{% set codeError = null %}
{% set cartProductIds = [] %}
{% set discountCodeOK = null %}

{# THIS CHECKS THE DISCOUNT CODE FOR IF THERE ARE ANY VALID/ELIGIBLE PRODUCTS IN THE CART #}
{% set currentDiscount = craft.commerce.discounts.discountByCode(cart.couponCode) %}
{% if currentDiscount %}
{% for item in cart.lineItems %}
    {% set cartProductIds = cartProductIds|merge([item.purchasable.id]) %}
{% endfor %}

    {% for product in currentDiscount.purchasableIds %}
        {% if product in cartProductIds  %}
            {% set discountCodeOK =  true %}
        {% endif %}
{% endfor %}

    {% if discountCodeOK != true %}
        {% set codeError = 'Sorry there are no products in your cart that are eligiable for the code ' ~ cart.couponCode %}
    {% endif %}
{% endif %}

{% if cart.getFirstError('couponCode') %}
&lt;br /&gt;&lt;br /&gt;
{% include '_includes/formError' with { error : cart.getFirstError('couponCode') } %}
{% else %}
{% if currentDiscount and codeError != null %}
    &lt;br /&gt;&lt;br /&gt;
    {% include '_includes/formError' with { error : codeError } %}
{% endif %}
{% endif %}

{% if currentDiscount and codeError == null %}
&lt;p class=&quot;color--success half-margin-top&quot;&gt;{{ cart.couponCode }} discount code has been applied to the cart.&lt;/p&gt;
{% endif %}
</code></pre>

CC BY-SA 4.0
2021-02-17T13:38:03.713Z
2021-02-17T13:38:03.713Z
Terry Upton
<p>I am trying to setup some error trapping on the discount code to output an error message if there are no products that qualify for the discount code the user enters. Here is the code I have;</p>

<pre><code>    {% set currentDiscount = craft.commerce.getDiscountByCode(cart.couponCode)%}

    {% for item in cart.lineItems %}
        {% set cartProductIds = item.purchasable.product.id %}

        {% if currentDiscount %}
            {% if not cartProductIds in currentDiscount.productIds %}You have no products in the cart that meet this discount{% endif %}
        {% endif %}
    {% endfor %}
</code></pre>

<p>Currently this doesn't give me anything back. Can anyone point me further in the right direction...?</p>

<p>Edit: I have since found the code I pasted as an answer no longer works or never fully worked. I am struggling to loop through an array and check against the other array and if no id's match then set an error. I want to check if any of the cart Id's match against any of the discount code id's.</p>

<p>Here is my revised code, but it just doesn't match fully. I don;t think it is a bug, but more likely a failing of my code. Can anyone shed any light?</p>

<pre><code>{# THIS CHECKS THE DICOUNT CODE FOR IF THERE ARE ANY VALID/ELIGABLE PRODUCTS IN THE CART #}
        {% set currentDiscount = craft.commerce.getDiscountByCode(cart.couponCode) %}
        {% set codeError = null %}

        {% if currentDiscount %}
            {% set cartProductIds = [] %}
            {% set discountProductsIds = [] %}

            {% for item in cart.lineItems %}
                {% set cartProductIds = cartProductIds|merge([item.purchasable.product.id]) %}
            {% endfor %}

            {% set cartProductIds = cartProductIds|join('|') %}

            {% for product in currentDiscount.products %}
                {% set discountProductsIds = discountProductsIds|merge([product.id]) %}
            {% endfor %}

            {% set discountProductsIds = discountProductsIds|join('|') %}

            Products in Cart = {{cartProductIds}}
            Products in Discount = {{discountProductsIds}}

            {% if cartProductIds not in discountProductsIds %}
                {% set codeError = 'Sorry there are no products in your cart that are eligiable for the code ' ~ cart.couponCode %}
            {% endif %}

        {% endif %}
</code></pre>

<plugin-craftcommerce><discounts>
Check if cart contains valid products for discount code
# Check if cart contains valid products for discount code

(Asked by: username_0 on 2016-09-27)

I am trying to setup some error trapping on the discount code to output an error message if there are no products that qualify for the discount code the user enters. Here is the code I have;
    {% set currentDiscount = craft.commerce.getDiscountByCode(cart.couponCode)%}

    {% for item in cart.lineItems %}
        {% set cartProductIds = item.purchasable.product.id %}

        {% if currentDiscount %}
            {% if not cartProductIds in currentDiscount.productIds %}You have no products in the cart that meet this discount{% endif %}
        {% endif %}
    {% endfor %}

Currently this doesn't give me anything back. Can anyone point me further in the right direction...?
Edit: I have since found the code I pasted as an answer no longer works or never fully worked. I am struggling to loop through an array and check against the other array and if no id's match then set an error. I want to check if any of the cart Id's match against any of the discount code id's.
Here is my revised code, but it just doesn't match fully. I don;t think it is a bug, but more likely a failing of my code. Can anyone shed any light?
{# THIS CHECKS THE DICOUNT CODE FOR IF THERE ARE ANY VALID/ELIGABLE PRODUCTS IN THE CART #}
        {% set currentDiscount = craft.commerce.getDiscountByCode(cart.couponCode) %}
        {% set codeError = null %}

        {% if currentDiscount %}
            {% set cartProductIds = [] %}
            {% set discountProductsIds = [] %}

            {% for item in cart.lineItems %}
                {% set cartProductIds = cartProductIds|merge([item.purchasable.product.id]) %}
            {% endfor %}

            {% set cartProductIds = cartProductIds|join('|') %}

            {% for product in currentDiscount.products %}
                {% set discountProductsIds = discountProductsIds|merge([product.id]) %}
            {% endfor %}

            {% set discountProductsIds = discountProductsIds|join('|') %}

            Products in Cart = {{cartProductIds}}
            Products in Discount = {{discountProductsIds}}

            {% if cartProductIds not in discountProductsIds %}
                {% set codeError = 'Sorry there are no products in your cart that are eligiable for the code ' ~ cart.couponCode %}
            {% endif %}

        {% endif %}

**Tags:** plugin-craftcommerce, discounts

---

## 3 answers

### Answer by username_1 on 2016-09-27. Score: 5

Try something like this:
{% set currentDiscount = craft.commerce.getDiscountByCode(cart.couponCode) %}

{% if currentDiscount %}

{% set cartProductIds = [] %}

{% for item in cart.lineItems %}
  {% set cartProductIds = cartProductIds|merge([item.purchasable.product.id]) %}
{% endfor %}

{% for product in currentDiscount.products %}
  {% if not product.id in cartProductIds %}{{ product.title }} not in cart.{% endif %}
{% endfor %}

{% endif %}

> **Comments:**
> **username_0**: Thanks @lukeholder. But it throws an error Variable "cartProductIds" does not exist ?  
> **username_2**: That will be a scope error - with the mergeg filter the variable must already exist I believe - I've adjusted the code above to create the empty array, then new Ids are merged in to it as before.  
> **username_0**: Thanks @JeremyDaalder - this no longer throws up a Craft error but I still can't get it to give me any feedback?  Each time I try to check if something is set (for example) {{currentDiscount}} or {{cartProductIds}} to check if they are set and have values, then I get an error like; Array to string conversion.  I'm a bit stuck with this one.  
> **username_0**: Just realised I need to do a '{{ dump(currentDiscount) }}' as it is an array.  

---

### Answer by username_0 on 2016-09-28. Score: 1

So some persistence I have this working as follows:
{% set currentDiscount = craft.commerce.getDiscountByCode(cart.couponCode) %}

{% if currentDiscount %}
    {% set cartProductIds = [] %}
    {% set codeError = null %}

    {% for item in cart.lineItems %}
        {% set cartProductIds = cartProductIds|merge([item.purchasable.product.id]) %}
    {% endfor %}

    {% for product in currentDiscount.products %}
        {% if not cartProductIds in product %}
            {% set codeError = 'true' %}
        {% endif %}
    {% endfor %}

    {% if codeError == 'true' %}
        
        {% include '_includes/formError' with { error : 'Sorry there are no products in your cart that are eligiable for the code ' ~ cart.couponCode } %}
    {% endif %}
{% endif %}

Sure the code can be cleaned up a little. So feel free to refine if anyone wishes to make it better.


---

### Answer by username_0 on 2021-02-17. Score: 1

I have now updated this code for Craft Commerce 2:
{% set codeError = null %}
{% set cartProductIds = [] %}
{% set discountCodeOK = null %}

{# THIS CHECKS THE DISCOUNT CODE FOR IF THERE ARE ANY VALID/ELIGIBLE PRODUCTS IN THE CART #}
{% set currentDiscount = craft.commerce.discounts.discountByCode(cart.couponCode) %}
{% if currentDiscount %}
{% for item in cart.lineItems %}
    {% set cartProductIds = cartProductIds|merge([item.purchasable.id]) %}
{% endfor %}

    {% for product in currentDiscount.purchasableIds %}
        {% if product in cartProductIds  %}
            {% set discountCodeOK =  true %}
        {% endif %}
{% endfor %}

    {% if discountCodeOK != true %}
        {% set codeError = 'Sorry there are no products in your cart that are eligiable for the code ' ~ cart.couponCode %}
    {% endif %}
{% endif %}

{% if cart.getFirstError('couponCode') %}

{% include '_includes/formError' with { error : cart.getFirstError('couponCode') } %}
{% else %}
{% if currentDiscount and codeError != null %}
    
    {% include '_includes/formError' with { error : codeError } %}
{% endif %}
{% endif %}

{% if currentDiscount and codeError == null %}
{{ cart.couponCode }} discount code has been applied to the cart.
{% endif %}


---

