Track incoming donations in a store metafield, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Track incoming donations in a store metafield

by Isaac Bowen (team@usemechanic.com)

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.

15-day free trial – unlimited tasks

Documentation

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.

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.

Events
when a user triggers the task (mechanic/user/trigger)
when an order is created (shopify/orders/create)
when an order is paid (shopify/orders/paid)
Options
metafield namespace (required), metafield key (required), donation product handle (required), recalculate nightly (boolean)
Script
{% 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 %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.