Auto-cancel orders with too many of a certain SKU, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-cancel orders with too many of a certain SKU

by Isaac Bowen (team@usemechanic.com)

Useful for enforcing order limits on the backend, use this task to ensure that too many of a certain product cannot be purchased. This task can be configured with multiple SKUs, too!

Runs when an order is created. Configuration includes skus to watch for, quantity threshold for cancellation, send cancellation email to customer, attempt to refund or void payment in full, and reason for cancellation.

15-day free trial – unlimited tasks

Documentation

Notes:

  • SKUs are case-sensitive!
  • The total quantity will be calculated by adding the quantities from each line item in the order, having a SKU that matches the SKU(s) you provide.
  • Any refunds generated by this task will be for the maximum amount, for the order being cancelled.

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 created (shopify/orders/create)
Options
skus to watch for (required, array), quantity threshold for cancellation (number, required), send cancellation email to customer (boolean), attempt to refund or void payment in full (boolean), reason for cancellation (required)
Script
{% comment %}
  An opinion about option order:

  {{ options.skus_to_watch_for__required_array }}
  {{ options.quantity_threshold_for_cancellation__number_required }}
  {{ options.send_cancellation_email_to_customer__boolean }}
  {{ options.attempt_to_refund_or_void_payment_in_full__boolean }}
  {{ options.reason_for_cancellation__required }}
{% endcomment %}

{% assign valid_reasons = "customer,inventory,fraud,declined,other" | split: "," %}
{% if options.reason_for_cancellation__required != blank %}
  {% unless valid_reasons contains options.reason_for_cancellation__required %}
    {"error": {{ valid_reasons | json | prepend: "'Reason for cancellation' must be one of " | json }}}
  {% endunless %}
{% endif %}

{% assign sku_count = 0 %}

{% for line_item in order.line_items %}
  {% if line_item.sku != blank and options.skus_to_watch_for__required_array contains line_item.sku %}
    {% assign sku_count = sku_count | plus: line_item.quantity %}
  {% endif %}
{% endfor %}

{% if event.preview or sku_count >= options.quantity_threshold_for_cancellation__number_required %}
  {% action "shopify" %}
      [
        "post",
        "/admin/orders/{{ order.id }}/cancel.json",
        {
          {% if options.attempt_to_refund_or_void_payment_in_full__boolean %}
            {% capture refund_query %}
              query {
                order(id: {{ order.admin_graphql_api_id | json }}) {
                  suggestedRefund(suggestFullRefund: true) {
                    amountSet {
                      shopMoney {
                        amount
                        currencyCode
                      }
                    }
                  }
                }
              }
            {% endcapture %}
            {% assign refund_result = refund_query | shopify %}
            {% assign refund_money = refund_result.data.order.suggestedRefund.amountSet.shopMoney %}

            {% if event.preview %}
              {% assign refund_money = hash %}
              {% assign refund_money["amount"] = order.total_price %}
              {% assign refund_money["currencyCode"] = order.currency %}
            {% endif %}

            "amount": {{ refund_money.amount | json }},
            "currency": {{ refund_money.currencyCode | json }},
          {% endif %}
          "email": {{ options.send_cancellation_email_to_customer__boolean | json }},
          "reason": {{ options.reason_for_cancellation__required | 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
Reason for cancellation
other