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

Mechanic is a development and ecommerce automation 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 Occurs whenever a product is created and Occurs whenever a product is updated, or whenever a product is ordered, or whenever a variant is added, removed, or updated. Configuration includes delivery profile id 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, 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/create
shopify/products/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
delivery profile id (required, number), required product tag (required)
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Delivery profile ID
1234567890
Required product tag
special