Reissue single-use discount codes after order cancellation with Mechanic.

Mechanic is an automation and development platform for Shopify. :)

Reissue single-use discount codes after order cancellation

by Isaac Bowen (team@usemechanic.com)

This task watches for cancellations of orders that have a single-use discount code. When one is found, this task creates a new single-use discount code, for the related customer, and emails it to them.

Runs when an order is cancelled. Configuration includes email subject and email body.

15-day free trial – unlimited tasks

Documentation

This task watches for cancellations of orders that have a single-use discount code. When one is found, this task creates a new single-use discount code, for the related customer, and emails it to them.

This task does not support discounts that are limited to certain collections, products, or shipping lines.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

Events
when an order is cancelled (shopify/orders/cancelled)
Options
email subject (required), email body (required, multiline)
Script
{% assign discount_code_string = order.discount_codes.first.code %}
{% assign discount_code = shop.discount_codes[discount_code_string] %}
{% assign price_rule = discount_code.price_rule %}

{% assign reissued_discount_code = discount_code_string | append: order.order_number %}

{% if event.preview and discount_code_string == blank %}
  {% assign reissued_discount_code = "DISCOUNT" | append: order.order_number %}
{% endif %}

{% capture query %}
  query {
    node(id: {{ price_rule.admin_graphql_api_id | json }}) {
      ... on PriceRule {
        allocationLimit
        allocationMethod
        oncePerCustomer
        target
        features
        itemEntitlements {
          targetAllLineItems
        }
        shippingEntitlements {
          targetAllShippingLines
        }
        validityPeriod {
          start
          end
        }
        valueV2 {
          ... on PricingPercentageValue {
            percentage
          }
          ... on MoneyV2 {
            amount
            currencyCode
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "node": {
          "allocationLimit": null,
          "allocationMethod": "ACROSS",
          "oncePerCustomer": true,
          "target": "LINE_ITEM",
          "features": [],
          "itemEntitlements": {
            "targetAllLineItems": true
          },
          "shippingEntitlements": {
            "targetAllShippingLines": false
          },
          "validityPeriod": {
            "start": "2019-08-21T04:52:54Z",
            "end": null
          },
          "valueV2": {
            "percentage": -50
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = result_json | parse_json %}
{% endif %}

{% assign priceRule = result.data.node %}

{% if priceRule == nil %}
  {"log": "No price rule found."}
{% elsif priceRule.oncePerCustomer != true %}
  {"error": "This price rule can be used multiple times per customer, and so is not applicable."}
{% elsif priceRule.features != empty %}
  {"error": "This price rule has extra features, and is not supported."}
  {"log": {{ priceRule.features | json }}}
{% elsif priceRule.target == "LINE_ITEM" and priceRule.itemEntitlements.targetAllLineItems != true %}
  {"error": "This price rule is limited to certain line items, and is not supported."}
{% elsif priceRule.target == "SHIPPING_LINE" and priceRule.shippingEntitlements.targetAllShippingLines != true %}
  {"error": "This price rule is limited to certain shipping lines, and is not supported."}
{% else %}
  {% action "shopify" %}
    mutation {
      priceRuleCreate(
        priceRule: {
          title: {{ reissued_discount_code | json }}
          oncePerCustomer: true
          value: {
            {% if priceRule.valueV2.percentage %}
              percentageValue: {{ priceRule.valueV2.percentage | json }}
            {% else %}
              fixedAmountValue: {{ priceRule.valueV2.amount | json }}
            {% endif %}
          }
          customerSelection: {
            forAllCustomers: false
            customerIdsToAdd: [{{ order.customer.admin_graphql_api_id | json }}]
          }
          target: {{ priceRule.target }}
          {% if priceRule.target == "LINE_ITEM" %}
            itemEntitlements: {
              targetAllLineItems: true
            }
          {% elsif priceRule.target == "SHIPPING_ITEM" %}
            shippingEntitlements: {
              targetAllShippingLines: true
            }
          {% endif %}
          allocationMethod: {{ priceRule.allocationMethod }}
          allocationLimit: {{ priceRule.allocationLimit | json }}
          validityPeriod: {
            start: {{ priceRule.validityPeriod.start | json }}
            {% if priceRule.validityPeriod.end != blank %}end: {{ priceRule.validityPeriod.end | json }}{% endif %}
          }
        }
        priceRuleDiscountCode: {
          code: {{ reissued_discount_code | json }}
        }
      ) {
        priceRuleDiscountCode {
          id
          code
        }
        priceRuleUserErrors {
          code
          field
          message
        }
      }
    }
  {% endaction %}

  {% action "email" %}
    {
      "to": {{ order.email | json }},
      "subject": {{ options.email_subject__required | json }},
      "body": {{ options.email_body__required_multiline | replace: "REISSUED_DISCOUNT_CODE", reissued_discount_code | newline_to_br | json }},
      "reply_to": {{ shop.customer_email | json }},
      "from_display_name": {{ shop.name | json }}
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Email subject
We've issued you a new discount code
Email body
Hello,

Now that order {{ order.name }} has been cancelled, we wanted to make sure you have another chance to use your discount.

Your new discount code: REISSUED_DISCOUNT_CODE

Thanks,
{{ shop.name }}