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

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

Auto-cancel orders with too many of a certain SKU

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 Occurs whenever 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

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!

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, 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/create
Tasks use subscriptions to sign up for specific kinds of events. Learn more
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)
Code
{% 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 order.fulfillment_status == "fulfilled" or order.fulfillment_status == "partial" %}
  {% assign order_qualifies = false %}
{% else %}
    {% assign order_qualifies = true %}
{% endif %}

{% if event.preview or sku_count >= options.quantity_threshold_for_cancellation__number_required %}
  {% if order_qualifies %}
    {% 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, refundShipping: 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 %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Reason for cancellation
other