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.
Try this taskWhen it runs
- Occurs whenever a product is updated, ordered, or variants are added, removed or updated
- Occurs when a user manually triggers the task
What you choose
- Metafield namespace and key
- Metafield value to match
- Delivery profile ID
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.
IMPORTANT:
If Shopify has migrated this shop to market-based shipping, this task will no longer be able to update the old shipping profiles. Shopify should have created new shipping collections during the migration. Whether you choose to use the auto-created shipping collections or configure your own is up to you. One suggestion would be to use a variant metafield condition using the metafield and value configured in this task as the collection source instead of the individually added variants.
Migration of all shops is expected to complete by July 2027. You can safely delete this task after your shop has been migrated by Shopify.
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
shopify/products/update
mechanic/user/trigger
How task subscriptions work
Option types and flags
- Metafield namespace and key (required)
- Metafield value to match (required)
- Delivery profile ID (required, number)
Task 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 %}
{% comment %}
-- September 2026: check if this shop has been converted to market-based shipping; if so, then raise error
{% endcomment %}
{% capture query %}
query {
shop {
features {
marketDrivenShipping
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if result.shop.features.marketDrivenShipping %}
{% error "Shopify has migrated this shop to market-based shipping, which this task does not support. See the task docs for more info." %}
{% break %}
{% endif %}
{% 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 %}
Mechanic Liquid reference