Generate a product discount when a voucher product is purchased, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Generate a product discount when a voucher product is purchased

Use this task to generate discounts for future purchases. When a customer buys a "voucher" product, they will be emailed a single-use discount code for either a fixed amount of money or percentage off a purchase on a corresponding "entitled" product".

Runs Occurs whenever an order is paid and Occurs when a Mechanic action is performed. Configuration includes voucher product ids and entitled product ids, discount code prefix, discount fixed amount, discount percentage, email subject, and email body.

15-day free trial – unlimited tasks

Documentation

Use this task to generate discounts for future purchases. When a customer buys a "voucher" product, they will be emailed a single-use discount code for either a fixed amount of money or percentage off a purchase on a corresponding "entitled" product".

If a customer purchases more than one voucher product, they will receive more than one email, each containing a unique discount code.

Options

  • Voucher product IDs and entitled product IDs: Enter the IDs of the voucher products you're selling on the left, and the IDs of products you want to be discounted on the right. (Learn how to find the product IDs.)
  • Discount code prefix: A small piece of text to add to the beginning of the generated discount code.
  • Discount fixed amount: The money value to be subtracted. If you choose this option, you cannot choose a discount percentage.
  • Discount percentage: The percentage to be subtracted (e.g. 15). If you choose this option, you cannot choose a fixed discount amount.
  • Email subject, body: The content to email to the customer. May use the following variables:
    • CUSTOMER_FIRST_NAME
    • DISCOUNT_CODE
    • ENTITLED_PRODUCT_TITLE
    • ORDER_NAME
    • VOUCHER_PRODUCT_TITLE

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/orders/paid
mechanic/actions/perform
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
voucher product ids and entitled product ids (keyval, number, required), discount code prefix (required), discount fixed amount (number), discount percentage (number), email subject (required), email body (multiline, required)
Code
{% assign ve_product_ids_keyval = options.voucher_product_ids_and_entitled_product_ids__keyval_number_required %}
{% assign discount_code_prefix = options.discount_code_prefix__required %}
{% assign discount_fixed_amount = options.discount_fixed_amount__number %}
{% assign discount_percentage = options.discount_percentage__number %}
{% assign email_subject = options.email_subject__required %}
{% assign email_body = options.email_body__multiline_required %}

{% if discount_percentage == blank and discount_fixed_amount == blank %}
  {% error "Please fill either the discount percentage or discount fixed amount." %}

{% elsif discount_percentage != blank and discount_fixed_amount != blank %}
  {% error "Please choose between the discount percentage and discount fixed amount - only one is permitted." %}
{% endif %}

{% if event.topic == "shopify/orders/paid" %}
  {% if event.preview %}
    {% capture order_json %}
      {
        "email": "customer@example.com",
        "customer": {
          "id": 1234567890,
          "first_name": "TEST"
        },
        "line_items": [
          {
            "id": 1234567890,
            "product_id": {{ ve_product_ids_keyval.first.first }},
            "quantity": 1
          }
        ]
      }
    {% endcapture %}

    {% assign order = order_json | parse_json %}
  {% endif %}

  {% if order.customer == blank %}
    {% log "This order has no related customer. Skipping" %}
    {% break %}
  {% endif %}

  {% if order.email == blank %}
    {% log "This order has no email address. Skipping" %}
    {% break %}
  {% endif %}

  {% assign customer_id = order.customer.id | prepend: "gid://shopify/Customer/" %}

  {% for line_item in order.line_items %}
    {% assign voucher_product_title = line_item.title %}
    {% assign voucher_product_id = line_item.product_id %}
    {% assign voucher_product_id_string = voucher_product_id | append: "" %}
    {% assign entitled_product_id = ve_product_ids_keyval[voucher_product_id_string] %}

    {% if entitled_product_id == blank %}
      {% continue %}
    {% endif %}

    {% comment %}
      -- make sure the entitled product exists in the shop and get its title
    {% endcomment %}

    {% capture query %}
      query {
        product(id: {{ entitled_product_id | prepend: "gid://shopify/Product/" | json }}) {
          id
          title
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "product": {
              "id": "gid://shopify/Product/1234567890",
              "title": "Widget"
            }
          }
        }
      {% endcapture %}

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

    {% assign entitled_product = result.data.product %}

    {% if entitled_product == blank %}
      {% action "echo"
        __error: "Entitled product not found in the shop. Discount code will not be created.",
        entitled_product_id: entitled_product_id,
        voucher_product_id: voucher_product_id
      %}
      {% continue %}
    {% endif %}

    {% for n in (1..line_item.quantity) %}
      {% assign discount_code = line_item.id | append: n | split: "" | reverse | join: "" | slice: 0, 4 | append: order.order_number | base64 | replace: "=", "" | upcase | prepend: discount_code_prefix %}

      {% capture mutation %}
        mutation {
          discountCodeBasicCreate(
            basicCodeDiscount: {
              code: {{ discount_code | json }}
              customerSelection: {
                customers: {
                  add: {{ array | push: customer_id | json }}
                }
              }
              title: {{ discount_code | json }}
              startsAt: {{ "now" | date: "%FT%T%:z" | json }}
              customerGets: {
                items: {
                  products: {
                    productsToAdd: {{ array | push: entitled_product.id | json }}
                  }
                }
                value: {
                  {% if discount_percentage != blank %}
                    percentage: {{ discount_percentage | abs | divided_by: 100.0 | json }}
                  {% else %}
                    discountAmount: {
                      amount: {{ discount_fixed_amount | abs | times: 1.0 | json }}
                      appliesOnEachItem: false
                    }
                  {% endif %}
                }
              }
              usageLimit: 1
            }
          ) {
            codeDiscountNode {
              id
              codeDiscount {
                ... on DiscountCodeBasic {
                  codes(first: 1) {
                    nodes {
                      id
                      code
                    }
                  }
                  summary
                }
              }
            }
            userErrors {
              code
              extraInfo
              field
              message
            }
          }
        }
      {% endcapture %}

      {% action %}
        {
          "type": "shopify",
          "options": {
            "query": {{ mutation | json }}
          },
          "meta": {
            "customer_first_name": {{ order.customer.first_name | default: "there" | json }},
            "order_email": {{ order.email | json }},
            "order_name": {{ order.name | json }},
            "discount_code": {{ discount_code | json }},
            "entitled_product_title": {{ entitled_product.title | json }},
            "voucher_product_title": {{ voucher_product_title | json }}
          }
        }
      {% endaction %}
    {% endfor %}
  {% endfor %}

{% elsif event.topic == "mechanic/actions/perform" %}
  {% comment %}
    -- only process a successful discountCodeBasicCreate shopify action; the Error reporter task should be used to respond to failures
  {% endcomment %}

  {% unless action.type == "shopify" and action.run.ok == true %}
    {% break %}
  {% endunless %}

  {% assign customer_first_name = action.meta.customer_first_name %}
  {% assign order_email = action.meta.order_email %}
  {% assign order_name = action.meta.order_name %}
  {% assign discount_code = action.meta.discount_code %}
  {% assign entitled_product_title = action.meta.entitled_product_title %}
  {% assign voucher_product_title = action.meta.voucher_product_title %}

  {% comment %}
    -- send a customer email for the created discount code
  {% endcomment %}

  {% assign email_subject = email_subject
    | replace: 'CUSTOMER_FIRST_NAME', customer_first_name
    | replace: 'DISCOUNT_CODE', discount_code
    | replace: 'ENTITLED_PRODUCT_TITLE', entitled_product_title
    | replace: 'ORDER_NAME', order_name
    | replace: 'VOUCHER_PRODUCT_TITLE', voucher_product_title
  %}
  {% assign email_body = email_body
    | replace: 'CUSTOMER_FIRST_NAME', customer_first_name
    | replace: 'DISCOUNT_CODE', discount_code
    | replace: 'ENTITLED_PRODUCT_TITLE', entitled_product_title
    | replace: 'ORDER_NAME', order_name
    | replace: 'VOUCHER_PRODUCT_TITLE', voucher_product_title
  %}

  {% action "email" %}
    {
      "to": {{ order_email | json }},
      "subject": {{ email_subject | strip | json }},
      "body": {{ email_body | strip | newline_to_br | json }},
      "reply_to": {{ shop.customer_email | json }},
      "from_display_name": {{ shop.name | json }}
    }
  {% endaction %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Discount code prefix
HOORAY
Email subject
[Order ORDER_NAME] Thanks! Your discount is attached.
Email body
Hi CUSTOMER_FIRST_NAME,

Thanks for your order! Here's your discount code:

<code>DISCOUNT_CODE</code>

Your purchase of VOUCHER_PRODUCT_TITLE has earned you a one-time discount on a future order of ENTITLED_PRODUCT_TITLE.

Thanks,
The team at {{ shop.name }}