Demonstration: Order editing, with Mechanic.

Mechanic is a development platform for Shopify. :)

Demonstration: Order editing

This task demonstrates Shopify's ability to edit orders through the admin API. It's intended to be a starting place for developing more specific functionality. Out of the box, this task automatically adds a product to orders that (optionally) meet an order minimum.

Runs when an order is created and when an action is performed. Configuration includes minimum order total, variant id to add, quantity to add, notify customer, and staff note.

15-day free trial – unlimited tasks

Documentation

This task demonstrates Shopify's ability to edit orders through the admin API. It's intended to be a starting place for developing more specific functionality. Out of the box, this task automatically adds a product to orders that (optionally) meet an order minimum.

Configure this task using a variant ID. (To learn how to find this, see How do I find an ID for a product, collection, order, or something else?.)

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.

Open source
View on GitHub to contribute to this task
Events
when an order is created (shopify/orders/create)
when an action is performed (mechanic/actions/perform)
Options
minimum order total (number), variant id to add (number, required), quantity to add (number, required), notify customer (boolean), staff note (required)
Script
{% if event.preview %}
  {% assign order = hash %}
  {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
  {% assign order["total_price"] = options.minimum_order_total__number | plus: 1 | append: "" %}
{% endif %}

{% if event.topic == "shopify/orders/create" %}
  {% if options.minimum_order_total__number != blank %}
    {% assign total_price = order.total_price | times: 1 %}
    {% if total_price >= options.minimum_order_total__number %}
      {% assign order_qualifies = true %}
    {% else %}
      {% log message: "The order did not meet the order total minimum", minimum_order_total: options.minimum_order_total__number, actual_order_total: total_price %}
    {% endif %}
  {% else %}
    {% assign order_qualifies = true %}
  {% endif %}

  {% if order_qualifies %}
    {% action "shopify" %}
      mutation {
       orderEditBegin(id: {{ order.admin_graphql_api_id | json }}) {
          calculatedOrder{
            id
          }
        }
      }
    {% endaction %}
  {% endif %}
{% elsif event.topic == "mechanic/actions/perform" %}
  {% assign data = action.run.result.data %}

  {% if data.orderEditBegin %}
    {% assign calculated_order_id = data.orderEditBegin.calculatedOrder.id %}

    {% action "shopify" %}
      mutation {
        orderEditAddVariant(
          id: {{ calculated_order_id | json }}
          variantId: "gid://shopify/ProductVariant/{{ options.variant_id_to_add__number_required | round | json }}"
          quantity: {{ options.quantity_to_add__number_required | round | json }}
        ){
          calculatedOrder {
            id
            addedLineItems(first: 1) {
              edges {
                node {
                  id
                }
              }
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% elsif data.orderEditAddVariant %}
    {% assign calculated_order_id = data.orderEditAddVariant.calculatedOrder.id %}

    {% action "shopify" %}
      mutation {
        orderEditCommit(
          id: {{ calculated_order_id | json }}
          notifyCustomer: {{ options.notify_customer__boolean | json }}
          staffNote: {{ options.staff_note__required | json }}
        ) {
          order {
            id
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.