3

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 %}
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 %}
4
  • Thanks @lukeholder. But it throws an error Variable "cartProductIds" does not exist ? – Terry Upton Sep 27 '16 at 17:39
  • 1
    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 Sep 28 '16 at 3:05
  • 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 Sep 28 '16 at 8:14
  • Just realised I need to do a '{{ dump(currentDiscount) }}' as it is an array. – Terry Upton Sep 28 '16 at 8:21
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' %}
        <br />
        {% 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.

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') %}
<br /><br />
{% include '_includes/formError' with { error : cart.getFirstError('couponCode') } %}
{% else %}
{% if currentDiscount and codeError != null %}
    <br /><br />
    {% include '_includes/formError' with { error : codeError } %}
{% endif %}
{% endif %}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.