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.
Try this taskWhen it runs
- Occurs whenever an order is created
- Occurs when a Mechanic action is performed
What you choose
- Minimum order total
- Variant ID to add
- Quantity to add
- Notify customer
- Staff note
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?.)
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
shopify/orders/create
mechanic/actions/perform
How task subscriptions work
Option types and flags
- Minimum order total (number)
- Variant ID to add (number, required)
- Quantity to add (number, required)
- Notify customer (boolean)
- Staff note (required)
Task code
{% 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 Liquid reference