Mechanic is a development and ecommerce automation platform for Shopify. :)
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 Occurs whenever a product is created.
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?)
shopify/products/create
{% comment %}
-- get all locations in the shop (up to 1K); by default legacy locations (fulfillment services) and inactive locations are excluded from results
{% endcomment %}
{% assign cursor = nil %}
{% assign locations = array %}
{% for n in (1..4) %}
{% capture query %}
query {
locations(
first: 250
after: {{ cursor | json }}
query: {{ search_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
name
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"locations": {
"nodes": [
{
"id": "gid://shopify/Location/1234567890"
},
{
"id": "gid://shopify/Location/2345678901"
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign locations = locations | concat: result.data.locations.nodes %}
{% if result.data.locations.pageInfo.hasNextPage %}
{% assign cursor = result.data.locations.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% comment %}
-- if there is only one active location configured in the shop, then there will be nothing for this task to do
{% endcomment %}
{% if locations.size == 1 %}
{% break %}
{% endif %}
{% comment %}
-- get all variants for this product (up to 2K)
{% endcomment %}
{% assign cursor = nil %}
{% assign variants = array %}
{% for n in (1..8) %}
{% capture query %}
query {
product(id: {{ product.admin_graphql_api_id | json }}) {
variants(
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
inventoryItem {
id
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"product": {
"variants": {
"nodes": [
{
"id": "gid://shopify/ProductVariant/1234567890",
"inventoryItem": {
"id": "gid://shopify/inventoryItem/1234567890"
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign product = result.data.product %}
{% assign variants = variants | concat: result.data.product.variants.nodes %}
{% if result.data.product.variants.pageInfo.hasNextPage %}
{% assign cursor = result.data.product.variants.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% comment %}
-- activate each variant's inventory at all active locations
{% endcomment %}
{% for variant in variants %}
{% action "shopify" %}
mutation {
inventoryBulkToggleActivation(
inventoryItemId: {{ variant.inventoryItem.id | json }}
inventoryItemUpdates: [
{% for location in locations %}
{
locationId: {{ location.id | json }}
activate: true
}
{% endfor %}
]
) {
inventoryItem {
id
}
inventoryLevels {
location {
id
name
}
}
userErrors {
field
message
code
}
}
}
{% endaction %}
{% endfor %}