Mechanic Get Mechanic
Task library

Create a product inventory CSV feed

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

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs every day at midnight (in local time)
  • Occurs every day at midnight (in local time), with a 12 hour delay

What you choose

Filter by product type

Documentation

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

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

Tutorial: Creating scheduled CSV feeds

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

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

mechanic/user/trigger
mechanic/scheduler/daily
mechanic/scheduler/daily+12.hours
How task subscriptions work

Task code

{% 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",
        "type": "multi_line_text_field",
        "value": {{ csv | csv | json }}
      }
    ]
  }
}
Mechanic Liquid reference

Default option values

Filter by product type
Shirt