Auto-recurring draft orders, with Mechanic.

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

Auto-recurring draft orders

This task searches for draft orders having the configured tag, and duplicates each one (minus the tag used for searching). Optionally, this task can automatically send an invoice to the customer on file, after the new draft order is created. Or, this task can auto-complete the order, marking the order as paid if you so chose. (Auto-completed orders will result in an email receipt being sent to the customer on file for the original draft order.)

Runs Occurs when a user manually triggers the task and Occurs every day at midnight (in local time). Configuration includes draft order tag, cycle start date, number of days in cycle, complete the order after creating, complete the order and mark as paid after creating, send email invoice after creating, email invoice subject, email invoice bcc, and email invoice custom message.

15-day free trial – unlimited tasks

Documentation

This task searches for draft orders having the configured tag, and duplicates each one (minus the tag used for searching). Optionally, this task can automatically send an invoice to the customer on file, after the new draft order is created. Or, this task can auto-complete the order, marking the order as paid if you so chose. (Auto-completed orders will result in an email receipt being sent to the customer on file for the original draft order.)

Run this task manually to scan and duplicate matching draft orders. Use the "run" task options to control automatic running of this task.

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
mechanic/user/trigger
mechanic/scheduler/daily
{% if options.send_email_invoice_after_creating__boolean or options.complete_the_order_after_creating__boolean or options.complete_the_order_and_mark_as_paid_after_creating__boolean %}shopify/draft_orders/create{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
draft order tag (required), cycle start date (required), number of days in cycle (number, required), complete the order after creating (boolean), complete the order and mark as paid after creating (boolean), send email invoice after creating (boolean), email invoice subject, email invoice bcc (email, array), email invoice custom message (multiline)
Code
{% assign draft_order_tag = options.draft_order_tag__required %}
{% assign cycle_start_date = options.cycle_start_date__required %}
{% assign number_of_days_in_cycle = options.number_of_days_in_cycle__number_required %}
{% assign complete_the_order_after_creating = options.complete_the_order_after_creating__boolean %}
{% assign complete_the_order_and_mark_as_paid_after_creating = options.complete_the_order_and_mark_as_paid_after_creating__boolean %}
{% assign send_email_invoice_after_creating = options.send_email_invoice_after_creating__boolean %}
{% assign email_invoice_subject = options.email_invoice_subject %}
{% assign email_invoice_bcc = options.email_invoice_bcc__email_array %}
{% assign email_invoice_custom_message = options.email_invoice_custom_message__multiline %}

{% if send_email_invoice_after_creating and complete_the_order_after_creating %}
  {% error "Choose either an email invoice or completing the order - not both. :)" %}
{% elsif send_email_invoice_after_creating and complete_the_order_and_mark_as_paid_after_creating %}
  {% error "Choose either an email invoice or completing the order - not both. :)" %}
{% endif %}

{% if complete_the_order_after_creating or complete_the_order_and_mark_as_paid_after_creating %}
  {% assign autocomplete = true %}
{% endif %}

{% assign cycle_start = cycle_start_date | date: "%s" %}

{% if cycle_start == blank or cycle_start == cycle_start_date %}
  {% error %}
    {{ cycle_start_date | json | prepend: "Mechanic wasn't able to parse " | append: " - try another value." | json }}
  {% enderror %}
{% endif %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% assign run_qualifies = false %}

  {% if event.preview %}
    {% action "shopify" %}
      mutation {
        draftOrderCreate(
          input: {
            billingAddress: {
              address1: "123 State St"
              city: "Everyville"
              company: "Widgets Inc"
              countryCode: EE
              firstName: "Victor"
              lastName: "Juarez"
              provinceCode: "PK"
              zip: "54321"
            }
            purchasingEntity: {
              customerId: "gid://shopify/Customer/1234567890"
            }
            email: "customer@example.com"
            lineItems: [
              {
                weight: {
                  unit: GRAMS
                  value: 5
                }
                originalUnitPriceWithCurrency: {
                  amount: "5.0"
                  currencyCode: USD
                }
                quantity: 2
                requiresShipping: false
                sku: "123ABC"
                taxable: true
                title: "Widget, Our Best"
              }
            ]
            tags: []
            taxExempt: false
            useCustomerDefaultAddress: false
          }
        ) {
          draftOrder {
            id
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}

  {% else %}
    {% assign cycle_start_date = cycle_start_date %}
    {% assign cycle_start_d    = cycle_start_date | date: "%s" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %}
    {% assign now_d            = "now"            | date: "%s" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %}
    {% assign day_gap          = now_d | minus: cycle_start_d | modulo: number_of_days_in_cycle %}

    {% if day_gap == 0 %}
      {% assign run_qualifies = true %}
    {% else %}
      {% log %}
        {{ number_of_days_in_cycle | minus: day_gap | prepend: "Waiting another " | append: " day(s) for cycle to complete" | json }}
      {% endlog %}
    {% endif %}
  {% endif %}

  {% if run_qualifies %}
    {% for n in (0..10) %}
      {% capture query %}
        query {
          draftOrders(
            first: 100
            after: {{ cursor | json }}
            query: {{ "tag:" | append: draft_order_tag | json }}
            sortKey: NUMBER
          ) {
            pageInfo {
              hasNextPage
              endCursor
            }
            nodes {
              id
              appliedDiscount {
                amountSet {
                  shopMoney {
                    amount
                    currencyCode
                  }
                }
                description
                title
                value
                valueType
              }
              billingAddress {
                address1
                address2
                city
                company
                countryCodeV2
                firstName
                lastName
                phone
                provinceCode
                zip
              }
              customer {
                id
              }
              customAttributes {
                key
                value
              }
              email
              lineItems(first: 150) {
                nodes {
                  appliedDiscount {
                    amountSet {
                      shopMoney {
                        amount
                        currencyCode
                      }
                    }
                    description
                    title
                    value
                    valueType
                  }
                  customAttributes {
                    key
                    value
                  }
                  originalUnitPriceWithCurrency {
                    amount
                    currencyCode
                  }
                  quantity
                  sku
                  taxable
                  title
                  variant {
                    id
                  }
                  weight {
                    unit
                    value
                  }
                }
              }
              note2
              shippingAddress {
                address1
                address2
                city
                company
                countryCodeV2
                firstName
                lastName
                phone
                provinceCode
                zip
              }
              shippingLine {
                title
                shippingRateHandle
                originalPriceSet {
                  shopMoney {
                    amount
                    currencyCode
                  }
                }
              }
              tags
              taxExempt
            }
          }
        }
      {% endcapture %}

      {% assign result = query | shopify %}

      {% for draft_order in result.data.draftOrders.nodes %}
        {% action "shopify" %}
          mutation {
            draftOrderCreate(
              input: {
                {% if draft_order.appliedDiscount %}
                  appliedDiscount: {
                    amountWithCurrency: {
                      amount: {{ draft_order.appliedDiscount.amountSet.shopMoney.amount | json }}
                      currencyCode: {{ draft_order.appliedDiscount.amountSet.shopMoney.currencyCode }}
                    }
                    description: {{ draft_order.appliedDiscount.description | json }}
                    title: {{ draft_order.appliedDiscount.title | json }}
                    value: {{ draft_order.appliedDiscount.value }}
                    valueType: {{ draft_order.appliedDiscount.valueType }}
                  }
                {% endif %}
                {% if draft_order.billingAddress %}
                  billingAddress: {
                    address1: {{ draft_order.billingAddress.address1 | json }}
                    address2: {{ draft_order.billingAddress.address2 | json }}
                    city: {{ draft_order.billingAddress.city | json }}
                    company: {{ draft_order.billingAddress.company | json }}
                    countryCode: {{ draft_order.billingAddress.countryCodeV2 }}
                    firstName: {{ draft_order.billingAddress.firstName | json }}
                    lastName: {{ draft_order.billingAddress.lastName | json }}
                    phone: {{ draft_order.billingAddress.phone | json }}
                    provinceCode: {{ draft_order.billingAddress.provinceCode | json }}
                    zip: {{ draft_order.billingAddress.zip | json }}
                  }
                {% endif %}
                {% if draft_order.customer %}
                  purchasingEntity: {
                    customerId: {{ draft_order.customer.id | json }}
                  }
                {% endif %}
                {% if draft_order.customAttributes != empty %}
                  customAttributes: [
                    {% for custom_attribute in draft_order.customAttributes %}
                      {
                        key: {{ custom_attribute.key | json }}
                        value: {{ custom_attribute.value | json }}
                      }
                    {% endfor %}
                  ]
                {% endif %}
                {% if draft_order.email %}
                  email: {{ draft_order.email | json }}
                {% endif %}
                lineItems: [
                  {% for line_item in draft_order.lineItems.nodes %}
                    {
                      {% if line_item.appliedDiscount %}
                        appliedDiscount: {
                          amountWithCurrency: {
                            amount: {{ line_item.appliedDiscount.amountSet.shopMoney.amount | json }}
                            currencyCode: {{ line_item.appliedDiscount.amountSet.shopMoney.currencyCode }}
                          }
                          description: {{ line_item.appliedDiscount.description | json }}
                          title: {{ line_item.appliedDiscount.title | json }}
                          value: {{ line_item.appliedDiscount.value | json }}
                          valueType: {{ line_item.appliedDiscount.valueType }}
                        }
                      {% endif %}
                      {% if line_item.customAttributes != empty %}
                        customAttributes: [
                          {% for custom_attribute in line_item.customAttributes %}
                            {
                              key: {{ custom_attribute.key | json }}
                              value: {{ custom_attribute.value | json }}
                            }
                          {% endfor %}
                        ]
                      {% endif %}
                      {% if line_item.originalUnitPriceWithCurrency.amount %}
                        originalUnitPriceWithCurrency: {
                          amount: {{ line_item.originalUnitPriceWithCurrency.amount | json }}
                          currencyCode: {{ line_item.originalUnitPriceWithCurrency.currencyCode }}
                        }
                      {% endif %}
                      quantity: {{ line_item.quantity | json }}
                      {% if line_item.requiresShipping != nil %}
                        requiresShipping: {{ line_item.requiresShipping | json }}
                      {% endif %}
                      {% if line_item.sku %}
                        sku: {{ line_item.sku | json }}
                      {% endif %}
                      taxable: {{ line_item.taxable | json }}
                      title: {{ line_item.title | json }}
                      {% if line_item.variant %}
                        variantId: {{ line_item.variant.id | json }}
                      {% endif %}
                      {% if line_item.weight and line_item.weight.value != 0 %}
                        weight: {
                          value: {{ line_item.weight.value | json }}
                          unit: {{ line_item.weight.unit }}
                        }
                      {% endif %}
                    }
                  {% endfor %}
                ]
                {% if send_email_invoice_after_creating or autocomplete %}
                  metafields: [
                    {
                      namespace: "mechanic"
                      key: {% if send_email_invoice_after_creating %}"autoinvoice"{% else %}"autocomplete"{% endif %}
                      value: "true"
                      type: "boolean"
                    }
                  ]
                {% endif %}
                {% if draft_order.note2 != blank %}
                  note: {{ draft_order.note2 | json }}
                {% endif %}
                {% if draft_order.shippingAddress %}
                  shippingAddress: {
                    address1: {{ draft_order.shippingAddress.address1 | json }}
                    address2: {{ draft_order.shippingAddress.address2 | json }}
                    city: {{ draft_order.shippingAddress.city | json }}
                    company: {{ draft_order.shippingAddress.company | json }}
                    countryCode: {{ draft_order.shippingAddress.countryCodeV2 }}
                    firstName: {{ draft_order.shippingAddress.firstName | json }}
                    lastName: {{ draft_order.shippingAddress.lastName | json }}
                    phone: {{ draft_order.shippingAddress.phone | json }}
                    provinceCode: {{ draft_order.shippingAddress.provinceCode | json }}
                    zip: {{ draft_order.shippingAddress.zip | json }}
                  }
                {% endif %}
                {% if draft_order.shippingLine %}
                  shippingLine: {
                    priceWithCurrency: {
                      amount: {{ draft_order.shippingLine.originalPriceSet.shopMoney.amount | json }}
                      currencyCode: {{ draft_order.shippingLine.originalPriceSet.shopMoney.currencyCode }}
                    }
                    shippingRateHandle: {{ draft_order.shippingLine.shippingRateHandle | json }}
                    title: {{ draft_order.shippingLine.title | json }}
                  }
                {% endif %}
                tags: {{ draft_order.tags | remove_tag: draft_order_tag | json }}
                taxExempt: {{ draft_order.taxExempt | json }}
                useCustomerDefaultAddress: false
              }
            ) {
              draftOrder {
                id
              }
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endfor %}

      {% if result.data.draftOrders.pageInfo.hasNextPage %}
        {% assign cursor = result.data.draftOrders.pageInfo.endCursor %}
      {% else %}
        {% break %}
      {% endif %}
    {% endfor %}
  {% endif %}

{% elsif event.topic == "shopify/draft_orders/create" and send_email_invoice_after_creating %}
  {% capture query %}
    query {
      draftOrder(
        id: {{ draft_order.admin_graphql_api_id | json }}
      ) {
        metafield(
          namespace: "mechanic"
          key: "autoinvoice"
        ) {
          value
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview or result.data.draftOrder.metafield %}
    {% action "shopify" %}
      mutation {
        draftOrderInvoiceSend(
          id: {{ draft_order.admin_graphql_api_id | json }}
          email: {
            {% if email_invoice_subject != blank %}
              subject: {{ email_invoice_subject | json }}
            {% endif %}
            bcc: {{ email_invoice_bcc | json }}
            customMessage: {{ email_invoice_custom_message | json }}
          }
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}

{% elsif event.topic == "shopify/draft_orders/create" and autocomplete %}
  {% capture query %}
    query {
      draftOrder(
        id: {{ draft_order.admin_graphql_api_id | json }}
      ) {
        metafield(
          namespace: "mechanic"
          key: "autocomplete"
        ) {
          value
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview or result.data.draftOrder.metafield %}
    {% action "shopify" %}
      mutation {
        draftOrderComplete(
          id: {{ draft_order.admin_graphql_api_id | json }}
          paymentPending: {% if complete_the_order_and_mark_as_paid_after_creating %}false{% else %}true{% endif %}
        ) {
          draftOrder {
            order {
              id
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Draft order tag
repeat-me
Cycle start date
2024-01-01
Number of days in cycle
7