Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

Occurs when a user manually triggers the task

What you choose

  • Multiplier when calculating price
  • Product tag to monitor
  • Only process active products
Show 3 more
  • Run automatically for product updates
  • Run daily
  • Test mode

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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

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
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Multiplier when calculating price
0.8
Only process active products
true
Run automatically for product updates
false
Test mode
true