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

Mechanic is a development platform for Shopify. :)

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.

Runs when a product is created and when a product is updated. Configuration includes delivery profile and required product tag.

15-day free trial – unlimited tasks

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.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, 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.

Open source
View on GitHub to contribute to this task
Events
when a product is created (shopify/products/create)
when a product is updated (shopify/products/update)
Options
delivery profile (required, number), required product tag (required)
Script
{% 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 tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Delivery profile
1234567890
Required product tag
special