Auto-price products based on their compare-at prices, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-price products based on their compare-at prices

by Isaac Bowen (team@usemechanic.com)

Use this task when the pricing for your products can be automatically calculated by multiplying with compare-at prices.

Runs when a user triggers the task. Configuration includes multiplier when calculating price, required product tag, run automatically for product updates, run daily, and test mode.

15-day free trial – unlimited tasks

Documentation

Run this task manually to process your entire product catalog, optionally filtering by product tag. Or, choose to have this task run whenever products are updated, or run daily, at midnight in your store's local timezone.

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)
Options
multiplier when calculating price (number, required), required product tag, run automatically for product updates (boolean), run daily (boolean), test mode (boolean)
Script
{% comment %}
  Preferred option order:

  {{ options.multiplier_when_calculating_price__number_required }}
  {{ options.required_product_tag }}
  {{ options.run_automatically_for_product_updates__boolean }}
  {{ options.run_daily__boolean }}
  {{ options.test_mode__boolean }}
{% endcomment %}

{% if options.multiplier_when_calculating_price__number_required < 0 %}
  {% error "Price multiplier must be at least 0. :)" %}
{% endif %}

{% if event.topic contains "shopify/products/" %}
  {% if event.preview %}
    {% capture product_json %}
      {
        "tags": {{ options.required_product_tag | json }},
        "variants": [
          {
            "id": 1234567890,
            "admin_graphql_api_id": "gid://shopify/ProductVariant/1234567890",
            "sku": "ABC123",
            "price": "999.00",
            "compare_at_price": "10.00"
          }
        ]
      }
    {% endcapture %}

    {% assign product = product_json | parse_json %}
  {% endif %}

  {% assign product_tags = product.tags | split: ", " %}
  {% if options.required_product_tag == blank or product_tags contains options.required_product_tag %}
    {% assign summaries = array %}
    {% assign mutations = array %}

    {% for variant in product.variants %}
      {% if variant.compare_at_price == blank %}
        {% continue %}
      {% endif %}

      {% assign compare_at_price = variant.compare_at_price | times: 1 %}
      {% assign price = variant.price | times: 1 %}
      {% assign desired_price = compare_at_price | times: options.multiplier_when_calculating_price__number_required %}

      {% if desired_price != price %}
        {% if options.test_mode__boolean %}
          {% assign summary = hash %}
          {% assign summary["id"] = variant.id %}
          {% assign summary["sku"] = variant.sku %}
          {% assign summary["compare_at_price"] = compare_at_price %}
          {% assign summary["current_price"] = price %}
          {% assign summary["desired_price"] = desired_price %}
          {% assign summaries[summaries.size] = summary %}
        {% else %}
          {% capture mutation %}
            productVariantUpdate{{ forloop.index }}: productVariantUpdate(
              input: {
                id: {{ variant.admin_graphql_api_id | json }}
                price: {{ desired_price | append: "" | json }}
              }
            ) {
              productVariant {
                price
                compareAtPrice
              }
              userErrors {
                field
                message
              }
            }
          {% endcapture %}

          {% assign mutations[mutations.size] = mutation %}
        {% endif %}
      {% endif %}
    {% endfor %}

    {% if options.test_mode__boolean %}
      {% if summaries != empty %}
        {% action "echo" summaries %}
      {% endif %}
    {% elsif mutations != empty %}
      {% action "shopify" %}
        mutation {
          {{ mutations | join: newline }}
        }
      {% endaction %}
    {% endif %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% assign cursor = nil %}

  {% assign productVariants_query = nil %}
  {% if options.required_product_tag != blank %}
    {% assign productVariants_query = options.required_product_tag | json | prepend: "tag:" %}
  {% endif %}

  {% for n in (0..100) %}
    {% capture query %}
      query {
        productVariants(
          first: 250
          after: {{ cursor | json }}
          query: {{ productVariants_query | json }}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            cursor
            node {
              id
              displayName
              price
              compareAtPrice
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "productVariants": {
              "edges": [
                {
                  "node": {
                    "id": "gid://shopify/ProductVariant/31343133196349",
                    "displayName": "IPod Nano - 8GB",
                    "price": "999.00",
                    "compareAtPrice": "10.00"
                  }
                }
              ]
            }
          }
        }
      {% endcapture %}

      {% assign result = result_json | parse_json %}
    {% endif %}

    {% for productVariant_edge in result.data.productVariants.edges %}
      {% assign productVariant = productVariant_edge.node %}

      {% if productVariant.compareAtPrice == blank %}
        {% continue %}
      {% endif %}

      {% assign compare_at_price = productVariant.compareAtPrice | times: 1 %}
      {% assign price = productVariant.price | times: 1 %}
      {% assign desired_price = compare_at_price | times: options.multiplier_when_calculating_price__number_required %}

      {% if desired_price != price %}
        {% if options.test_mode__boolean %}
          {% action "echo" name: productVariant.displayName, compare_at_price: compare_at_price, current_price: price, desired_price: desired_price %}
        {% else %}
          {% action "shopify" %}
            mutation {
              productVariantUpdate(
                input: {
                  id: "gid://shopify/ProductVariant/31343133196349"
                  price: {{ desired_price | append: "" | json }}
                }
              ) {
                productVariant {
                  price
                  compareAtPrice
                }
                userErrors {
                  field
                  message
                }
              }
            }
          {% endaction %}
        {% endif %}
      {% endif %}
    {% endfor %}

    {% if result.data.productVariants.pageInfo.hasNextPage %}
      {% assign cursor = result.data.productVariants.edges.last.cursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Multiplier when calculating price
0.8
Test mode
true