Calculate total quantities purchased by SKU with Mechanic.

Mechanic is an automation and development platform for Shopify. :)

Calculate total quantities purchased by SKU

by Isaac Bowen (team@usemechanic.com)

This task scans all orders, regardless of status, and counts up the lifetime quantity purchased for each SKU, listing each SKU with its quantity and all related order names. This task demonstrates Shopify's bulk operations API (currently in beta).

Runs when a user triggers the task and when a bulk operation finishes.

15-day free trial – unlimited tasks

Documentation

This task scans all orders, regardless of status, and counts up the lifetime quantity purchased for each SKU, listing each SKU with its quantity and all related order names. This task demonstrates Shopify's bulk operations API (currently in beta).

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 a bulk operation finishes (mechanic/shopify/bulk_operation)
Script
{% if task.shopify_api_version != "unstable" %}
  {"error": "Set this task's Shopify API version to 'unstable' to continue"}
{% endif %}

{% if event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      orders {
        edges {
          node {
            id
            __typename
            name
            lineItems {
              edges {
                node {
                  id
                  __typename
                  quantity
                  sku
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% assign quantities_by_sku = hash %}
  {% assign order_names_by_sku = hash %}

  {% for object in bulkOperation.objects %}
    {% case object.__typename %}
    {% when "LineItem" %}
      {% assign sku = object.sku | default: "(no sku)" %}
      {% assign quantities_by_sku[sku] = quantities_by_sku[sku] | default: 0 | plus: object.quantity %}

      {% if order_names_by_sku[object.sku] == nil %}
        {% assign order_names_by_sku[sku] = array %}
      {% endif %}

      {% assign _count = order_names_by_sku[sku].size %}
      {% assign order_names_by_sku[sku][_count] = object.__parent.name %}
    {% endcase %}
  {% endfor %}

  {% if event.preview %}
    {% assign quantities_by_sku["ABC123"] = 20 %}
    {% assign order_names_by_sku["ABC123"] = "#12345,#67890" | split: "," %}
    {% assign quantities_by_sku["DEF456"] = 20 %}
    {% assign order_names_by_sku["DEF456"] = "#12345,#67890" | split: "," %}
  {% endif %}

  {% capture summary -%}
    Quantity purchased by SKU, across all time and all order statuses:

    {% for keyval in quantities_by_sku -%}
      {% assign sku = keyval[0] -%}
      {% assign quantity = keyval[1] -%}

      * {{ sku }}: {{ quantity }} ({{ order_names_by_sku[sku] | join: ", " }})
    {% endfor %}
  {% endcapture %}

  {% assign summary = summary | unindent | strip %}

  {% action "echo" summary %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.