Auto-associate variants with a delivery profile, by metafield value, with Mechanic.

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

Auto-associate variants with a delivery profile, by metafield value

Use this task to automatically add variants to the configured delivery profile on product update, if any of the variants have a metafield value matching the configured value. Conversely, variants with a non-existant or mismatched metafield value that are in the configured delivery profile will be removed.

Runs Occurs whenever a product is updated, ordered, or variants are added, removed or updated and Occurs when a user manually triggers the task. Configuration includes metafield namespace and key, metafield value to match, and delivery profile id.

15-day free trial – unlimited tasks

Documentation

Use this task to automatically add variants to the configured delivery profile on product update, if any of the variants have a metafield value matching the configured value. Conversely, variants with a non-existant or mismatched metafield value that are in the configured delivery profile will be removed.

Configure this task with a variant metafield namespace and key separated by a period (e.g. "custom.my_metafield"), the metafield value to match, and the delivery profile ID. Find the delivery profile ID by navigating to the "Shipping and delivery" section of your Shopify settings, and clicking on the "Manage " link of the delivery profile you want to use. The delivery profile ID is the series of numbers at the very end of the URL – if the URL is admin.shopify.com/store/{your_shop}/settings/shipping/profiles/12345, then the delivery profile ID is 123545.

As an initial setup, you can run the task manually to have it review all of the variants in your shop.

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
shopify/products/update
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
metafield namespace and key (required), metafield value to match (required), delivery profile id (required, number)
Code
{% assign metafield_namespace_and_key = options.metafield_namespace_and_key__required %}
{% assign metafield_value_to_match = options.metafield_value_to_match__required %}
{% assign delivery_profile_id = options.delivery_profile_id__required_number %}

{% capture query %}
  query {
    deliveryProfile(id: {{ "gid://shopify/DeliveryProfile/" | append: delivery_profile_id | json }}) {
      name
      id
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% assign delivery_profile = result.data.deliveryProfile %}

{% unless delivery_profile or event.preview %}
  {% error "Delivery profile not found! Please double-check the profile ID." %}
  {% break %}
{% endunless %}

{% if event.topic == "shopify/products/update" %}
  {% capture query %}
    query {
      product(id: {{ product.admin_graphql_api_id | json }}) {
        variants(first: 100) {
          nodes {
            id
            deliveryProfile {
              id
            }
            metafield(
              key: {{ metafield_namespace_and_key | json }}
            ) {
              value
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% assign variants = result.data.product.variants.nodes %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign cursor = nil %}  
  {% assign variants = array %}
  
  {% for n in (1..200) %}
    {% capture query %}
      query {
        productVariants(
          first: 250
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            id
            deliveryProfile {
              id
            }
            metafield(
              key: {{ metafield_namespace_and_key | json }}
            ) {
              value
            }
          }
        }
      }
    {% endcapture %}
  
    {% assign result = query | shopify %}
    
    {% assign variants
      = result.data.productVariants.nodes
      | default: array
      | concat: variants
    %}
  
    {% if result.data.productVariants.pageInfo.hasNextPage %}
      {% assign cursor = result.data.productVariants.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

{% if event.preview %}
  {% capture variants_json %}
    [
      {
        "id": "gid://shopify/ProductVariant/1234567890",
        "deliveryProfile": {
          "id": "gid://shopify/DeliveryProfile/1234567890"
        },
        "metafield": {
          "value": {{ metafield_value_to_match | json }}
        }
      }
    ]
  {% endcapture %}

  {% assign variants = variants_json | parse_json %}
{% endif %}

{% assign variant_actions_and_ids = array %}

{% for variant in variants %}
  {% unless variant.deliveryProfile %}
    {% comment %}
      -- digital or other type of product that does not require shipping; skip it
    {% endcomment %}
    {% continue %}
  {% endunless%}

  {% if variant.metafield.value == metafield_value_to_match %}
    {% if variant.deliveryProfile.id != delivery_profile.id %}
      {% assign variant_action_and_id = hash %}
      {% assign variant_action_and_id["action"] = "associate" %}
      {% assign variant_action_and_id["id"] = variant.id %}
      {% assign variant_actions_and_ids = variant_actions_and_ids | push: variant_action_and_id %}
    {% endif %}

  {% elsif variant.deliveryProfile.id == delivery_profile.id %}
    {% assign variant_action_and_id = hash %}
    {% assign variant_action_and_id["action"] = "dissociate" %}
    {% assign variant_action_and_id["id"] = variant.id %}
    {% assign variant_actions_and_ids = variant_actions_and_ids | push: variant_action_and_id %}
  {% endif %}
{% endfor %}

{% assign groups_of_variant_actions_and_ids = variant_actions_and_ids | in_groups_of: 250, fill_with: false %}

{% for group_of_variant_actions_and_ids in groups_of_variant_actions_and_ids %}
  {% assign variant_ids_to_associate
    = group_of_variant_actions_and_ids
    | where: "action", "associate"
    | map: "id"
  %}
  {% assign variant_ids_to_dissociate
    = group_of_variant_actions_and_ids
    | where: "action", "dissociate"
    | map: "id"
  %}

  {% action "shopify" %}
    mutation {
      deliveryProfileUpdate(
        id: {{ delivery_profile.id | json }}
        profile: {
          variantsToAssociate: {{ variant_ids_to_associate | json }}
          variantsToDissociate: {{ variant_ids_to_dissociate | json }}
        }
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}

{% else %}
  {% log 
    message: "No variants qualified to be added or removed from the configured delivery profile in this task run.",
    task_options_for_this_run: task.options
  %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more