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

Mechanic is a development and ecommerce automation platform for Shopify. :)

Auto-price products based on their compare-at prices

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

Runs Occurs when a user manually triggers the task. Configuration includes multiplier when calculating price, product tag to monitor, only process active products, run automatically for product updates, run daily, and test mode.

15-day free trial – unlimited tasks

Documentation

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

Run this task manually to process your entire product catalog, optionally filtering by active products or product tag. You may also 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, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
{% if options.run_automatically_for_product_updates__boolean %}
  shopify/products/update
{% endif %}
{% if options.run_daily__boolean %}
  mechanic/scheduler/daily
{% endif %}
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
multiplier when calculating price (number, required), product tag to monitor, only process active products (boolean), run automatically for product updates (boolean), run daily (boolean), test mode (boolean)
Code
{% assign multiplier_when_calculating_price = options.multiplier_when_calculating_price__number_required %}
{% assign product_tag_to_monitor = options.product_tag_to_monitor %}
{% assign only_process_active_products = options.only_process_active_products__boolean %}
{% assign run_automatically_for_product_updates = options.run_automatically_for_product_updates__boolean %}
{% assign run_daily = options.run_daily__boolean %}
{% assign test_mode = options.test_mode__boolean %}

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

{% assign search_query = nil %}

{% if only_process_active_products %}
  {% assign search_query = "product_status:active" %}
{% endif %}

{% if product_tag_to_monitor != blank %}
  {%- capture search_query -%}
    {{ search_query }} tag:{{ product_tag_to_monitor | json }}
  {%- endcapture -%}
{% endif %}

{% comment %}
  -- for product create/update filter the variants query with the product ID that caused the event
{% endcomment %}

{% if event.topic == "shopify/products/update" %}
  {%- capture search_query -%}
    product_id:{{ product.id }} {{ search_query }}
  {%- endcapture -%}
{% endif %}

{% unless event.preview %}
  {% log search_query: search_query %}
{% endunless %}

{% comment %}
  -- querying variants resource to support 2K variants per product without two concurrent pagination loops
{% endcomment %}

{% assign summaries = array %}
{% assign variant_inputs_by_product = hash %}

{% assign cursor = nil %}

{% for n in (0..200) %}
  {% capture query %}
    query {
      productVariants(
        first: 250
        after: {{ cursor | json }}
        query: {{ search_query | json }}
      ) {
        pageInfo {
          hasNextPage
          endCursor
        }
        nodes {
          id
          displayName
          sku
          price
          compareAtPrice
          product {
            id
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "productVariants": {
            "nodes": [
              {
                "id": "gid://shopify/ProductVariant/1234567890",
                "displayName": "Widget",
                "sku": "WDGT",
                "price": "99.00",
                "compareAtPrice": "10.00",
                "product": {
                  "id": "gid://shopify/Product/1234567890"
                }
              }
            ]
          }
        }
      }
    {% endcapture %}

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

  {% comment %}
    -- process this batch of variants to see which qualify to have their price updated
  {% endcomment %}

  {% for variant in result.data.productVariants.nodes %}
    {% if variant.compareAtPrice == blank %}
      {% continue %}
    {% endif %}

    {% assign compare_at_price = variant.compareAtPrice | times: 1 %}
    {% assign price = variant.price | times: 1 %}
    {% assign desired_price
      = compare_at_price
      | times: multiplier_when_calculating_price
      | round: 2
    %}

    {% if desired_price != price %}
      {% if test_mode %}
        {% assign summary = hash %}
        {% assign summary["id"] = variant.id %}
        {% assign summary["displayName"] = variant.displayName | remove: " - Default Title" %}
        {% 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 | push: summary %}

      {% else %}
        {% assign variant_input = hash %}
        {% assign variant_input["id"] = variant.id %}
        {% assign variant_input["price"] = desired_price | append: "" %}
        {% assign variant_inputs_by_product[variant.product.id]
          = variant_inputs_by_product[variant.product.id]
          | default: array
          | push: variant_input
        %}
      {% endif %}
    {% endif %}
  {% endfor %}

  {% if result.data.productVariants.pageInfo.hasNextPage %}
    {% assign cursor = result.data.productVariants.pageInfo.endCursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}

{% for keyval in variant_inputs_by_product %}
  {% action "shopify" %}
    mutation {
      productVariantsBulkUpdate(
        productId: {{ keyval[0] | json }}
        variants: {{ keyval[1] | graphql_arguments }}
      ) {
        userErrors {
          code
          field
          message
        }
      }
    }
  {% endaction %}

{% else %}
  {% log "No variants qualified to be updated on this task run." %}
{% endfor %}

{% if summaries != blank %}
  {% log
    test_mode: test_mode,
    variants_to_be_updated: summaries
  %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Multiplier when calculating price
0.8
Only process active products
true
Test mode
true