Auto-connect new products to all locations with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Auto-connect new products to all locations

by Isaac Bowen (team@usemechanic.com)

Using Shopify's multi-locations feature, and importing products in bulk? Use this task to automatically connect each new product to every location for your store.

Runs when a product is created. Configuration includes track inventory using shopify and allow orders for out of stock products.

15-day free trial – unlimited tasks

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.

Events
when a product is created (shopify/products/create)
Options
track inventory using shopify (boolean), allow orders for out of stock products (boolean)
Script
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "post",
        "/admin/inventory_levels/connect.json",
        {
          "inventory_item_id": 1234567890,
          "location_id": 1234567890,
          "relocate_if_necessary": false
        }
      ]
    }
  }
{% else %}
  {% for variant in product.variants %}
    {% for location in shop.locations %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "post",
            "/admin/inventory_levels/connect.json",
            {
              "inventory_item_id": {{ variant.inventory_item_id | json }},
              "location_id": {{ location.id | json }},
              "relocate_if_necessary": false
            }
          ]
        }
      }
    {% endfor %}
  {% endfor %}
{% endif %}

{% if event.preview or options.track_inventory_using_shopify__boolean or options.allow_orders_for_out_of_stock_products__boolean %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "product",
          {{ product.id | json }}
        ],
        {
          "variants": [
            {% for variant in product.variants %}
              {
                "id": {{ variant.id | json }},
                "inventory_management": {% if options.track_inventory_using_shopify__boolean %}"shopify"{% else %}null{% endif %},
                "inventory_policy": {% if options.allow_orders_for_out_of_stock_products__boolean %}"continue"{% else %}"deny"{% endif %}
              }{% unless forloop.last %},{% endunless %}
            {% endfor %}
          ]
        }
      ]
    }
  }
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Track inventory using shopify
true
Allow orders for out of stock products
true