Shopify Abandon Email – Check If User Used Existing Discount Code

Without using a Shopify app I needed a solution for checking if a customer has used a discount code before and if not, include one in the abandon email template.

How the code works:

It scans the order history of the customer and loops through the discounts to see if the coupon matches with the variable searchCoupon. If it does, break out of the loop and set usedCouponBefore to true.

{% assign searchCoupon = "TAKE5OFF" | upcase %}
{% assign usedCouponBefore = false %}
{% assign usedCouponOrder = null %}

{% for order in customer.orders %}
  {% for discount in order.discounts %}
    {% assign discountCode = discount.code | upcase %}
    {% if discountCode contains searchCoupon %}
      {% assign usedCouponBefore = true %}
      {% assign usedCouponOrder = order %}
      {% break %}
    {% endif %}
  {% endfor %}
  {% if usedCouponBefore == true %}
    {% break %}
  {% endif %}
{% endfor %}

{% if usedCouponBefore == false %}
  {% if url contains '?' %}
    {% assign shopUrl = url | append: '&discount=TAKE5OFF' %}
  {% else %}
    {% assign shopUrl = url | append: '?&discount=TAKE5OFF' %}
  {% endif %}
{% else %}
  {% assign shopUrl = url %}
{% endif %}

{% if usedCouponBefore == false %}
 <a href="{{ shopUrl }}">Take an additional 5% off</a>
{% endif %}

Possible issues:

I’m not sure what the default limit is for looping through customer’s orders is so if certain customers have a bunch of past orders, you may want to increase that limit in the for loop.

Leave a reply:

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Site Footer

Sliding Sidebar

RYAN OUN

Nice to meet you, my name is Ryan and I build stuff for the web. Welcome to my website where you can learn about me and my interests.

USEFUL LINKS