Mechanic is an automation development platform for Shopify. :)
This task watches for orders for a certain donation product, and tallies up the total donation amount in a store metafield, allowing you to display this value in your online storefront.
Runs when a user triggers the task, when an order is created, and when an order is paid. Configuration includes metafield namespace, metafield key, donation product handle, and recalculate nightly.
This task runs automatically, whenever an order is paid. To fully recalculate the total donation amount stored, click the "Run task" button. Optionally, choose to have this task run the recalculation nightly - useful for making sure that refunds are regularly accounted for.
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.
{% if event.preview %} { "action": { "type": "shopify", "options": [ "create", "metafield", { "namespace": {{ options.metafield_namespace__required | json }}, "key": {{ options.metafield_key__required | json }}, "value_type": "string", "value": "5.5" } ] } } {% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %} {% assign donation_total = 0 %} {% for order in shop.orders.any.paid %} {% for line_item in order.line_items %} {% if line_item.product.handle != options.donation_product_handle__required %} {% continue %} {% endif %} {% assign donation_total = donation_total | plus: line_item.price | minus: line_item.total_discount %} {% endfor %} {% endfor %} { "action": { "type": "shopify", "options": [ "create", "metafield", { "namespace": {{ options.metafield_namespace__required | json }}, "key": {{ options.metafield_key__required | json }}, "value_type": "string", "value": {{ donation_total | append: "" | json }} } ] } } {% elsif event.topic == "shopify/orders/create" or event.topic == "shopify/orders/paid" %} {% if order.financial_status == "paid" %} {% assign current_donation = 0 %} {% for line_item in order.line_items %} {% if line_item.product.handle != options.donation_product_handle__required %} {% continue %} {% endif %} {% assign current_donation = current_donation | plus: line_item.price | minus: line_item.total_discount %} {% endfor %} {% if current_donation > 0 %} { "action": { "type": "shopify", "options": [ "create", "metafield", { "namespace": {{ options.metafield_namespace__required | json }}, "key": {{ options.metafield_key__required | json }}, "value_type": "string", "value": {{ shop.metafields[options.metafield_namespace__required][options.metafield_key__required] | plus: current_donation | append: "" | json }} } ] } } {% endif %} {% endif %} {% endif %}