Mechanic Get Mechanic
Task library

Auto-associate products with a delivery profile, by product tag

Use this task to automatically add products, as they're tagged, to a specific delivery profile. Untag products to remove them from the configured profile.

Try this task

When it runs

  • Occurs whenever a product is created
  • Occurs whenever a product is updated, ordered, or variants are added, removed or updated

What you choose

  • Delivery profile ID
  • Required product tag

Documentation

Use this task to automatically add products, as they're tagged, to a specific delivery profile. Untag products to remove them from the configured profile.

Configure this task using a product tag to watch for, and a delivery profile ID. Find the delivery profile ID by navigating to the "Shipping and delivery" section of your Shopify admin area, and clicking on the "Manage rates" 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 example.myshopify.com/admin/settings/shipping/profiles/12345, then the delivery profile ID is 123545.

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 product tag condition using the tag configured in this task as the collection source instead of the individually added products.

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/create
shopify/products/update
How task subscriptions work

Option types and flags

Task code

{% 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.data.shop.features.marketDrivenShipping %}
  {% log result.data %}
  {% 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: options.delivery_profile_id__required_number | json }}) {
      name
      id
    }

    product(id: {{ product.admin_graphql_api_id | json }}) {
      id
      tags
      variants(first: 250) {
        pageInfo {
          hasNextPage
        }
        edges {
          node {
            id
            deliveryProfile {
              id
            }
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "deliveryProfile": {
          "name": "General Profile",
          "id": "gid://shopify/DeliveryProfile/1234567890"
        },
        "product": {
          "id": "gid://shopify/Product/1234567890",
          "tags": [{{ options.required_product_tag__required | json }}],
          "variants": {
            "pageInfo": {
              "hasNextPage": false
            },
            "edges": [
              {
                "node": {
                  "id": "gid://shopify/ProductVariant/1234567890",
                  "deliveryProfile": {
                    "id": "gid://shopify/DeliveryProfile/2345678901"
                  }
                }
              }
            ]
          }
        }
      }
    }
  {% endcapture %}

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

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

{% if result.data.deliveryProfile == nil %}
  {% error message: "Delivery profile not found! Please double-check the profile ID.", delivery_profile_id: options.delivery_profile_id__required_number %}
{% else %}
  {% log delivery_profile: deliveryProfile, product: product %}
{% endif %}

{% if product.variants.pageInfo.hasNextPage %}
  {% log "Warning: This product has more than 250 variants. Only the first 250 will be handled." %}
{% endif %}

{% assign variant_ids_to_associate = array %}
{% assign variant_ids_to_dissociate = array %}

{% for variant_edge in product.variants.edges %}
  {% assign variant = variant_edge.node %}

  {% if product.tags contains options.required_product_tag__required %}
    {% if variant.deliveryProfile.id != deliveryProfile.id %}
      {% assign variant_ids_to_associate[variant_ids_to_associate.size] = variant.id %}
    {% endif %}
  {% else %}
    {% if variant.deliveryProfile.id == deliveryProfile.id %}
      {% assign variant_ids_to_dissociate[variant_ids_to_dissociate.size] = variant.id %}
    {% endif %}
  {% endif %}
{% endfor %}

{% if variant_ids_to_associate != empty or variant_ids_to_dissociate != empty %}
  {% action "shopify" %}
    mutation {
      deliveryProfileUpdate(
        id: {{ deliveryProfile.id | json }}
        profile: {
          {% if variant_ids_to_associate != empty %}
            variantsToAssociate: {{ variant_ids_to_associate | json }}
          {% endif %}

          {% if variant_ids_to_dissociate != empty %}
            variantsToDissociate: {{ variant_ids_to_dissociate | json }}
          {% endif %}
        }
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic Liquid reference

Default option values

Delivery profile ID
1234567890
Required product tag
special