Create a product inventory feed, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Create a product inventory feed

by Isaac Bowen (team@usemechanic.com)

This task, paired with the linked instructions, creates a feed of product inventory (optionally filtering by product type).

Runs when a user triggers the task, every day at midnight, and 12 hours after every day at midnight. Configuration includes filter by product type.

15-day free trial – unlimited tasks

Documentation

This task creates a regularly-updated feed, available at yourstore.com/pages/feed. Follow these required instructions to configure your store:

Tutorial: Creating scheduled feeds with Mechanic

Note: This task fetches up to 30 variants per product.

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 user triggers the task (mechanic/user/trigger)
every day at midnight (mechanic/scheduler/daily)
12 hours after every day at midnight (mechanic/scheduler/daily+12.hours)
Options
filter by product type
Script
{% assign csv = array %}
{% assign csv[0] = "Handle,Title,Option1 Name,Option1 Value,Option2 Name,Option2 Value,Option3 Name,Option3 Value,SKU,Quantity" | split: "," %}

{% assign cursor = nil %}
{% for n in (0..1000) %}
  {% capture query %}
    query {
      products(
        first: 15
        after: {{ cursor | json }}
        sortKey: CREATED_AT
        query: {% if options.filter_by_product_type == blank %}null{% else %}{{ options.filter_by_product_type | json | prepend: "product_type:" | json }}{% endif %}
      ) {
        pageInfo {
          hasNextPage
        }
        edges {
          cursor
          node {
            handle
            title
            variants(first: 30) {
              edges {
                node {
                  sku
                  inventoryQuantity
                  selectedOptions {
                    name
                    value
                  }
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% for product_edge in result.data.products.edges %}
    {% assign product = product_edge.node %}

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

      {% assign row = array %}
      {% assign row[row.size] = product.handle %}
      {% assign row[row.size] = product.title %}
      {% assign row[row.size] = variant.selectedOptions[0].name %}
      {% assign row[row.size] = variant.selectedOptions[0].value %}
      {% assign row[row.size] = variant.selectedOptions[1].name %}
      {% assign row[row.size] = variant.selectedOptions[1].value %}
      {% assign row[row.size] = variant.selectedOptions[2].name %}
      {% assign row[row.size] = variant.selectedOptions[3].value %}
      {% assign row[row.size] = variant.sku %}
      {% assign row[row.size] = variant.inventoryQuantity %}

      {% assign csv[csv.size] = row %}
    {% endfor %}
  {% endfor %}

  {% if result.data.products.pageInfo.hasNextPage %}
    {% assign cursor = result.data.products.edges.last.cursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}

{
  "action": {
    "type": "shopify",
    "options": [
      "create",
      "metafield",
      {
        "namespace": "mechanic",
        "key": "feed",
        "value_type": "string",
        "value": {{ csv | csv | json }}
      }
    ]
  }
}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Filter by product type
Shirt