{
  "schema_version": "1.0.0",
  "id": "shopify-line-items-to-sheet",
  "title": "Write each Shopify line item to its own sheet row",
  "category": "ecommerce",
  "difficulty": "advanced",
  "featured": false,
  "description": "Split a Shopify order into one sheet row per product, after a Code step confirms the comma-joined line items still line up.",
  "tags": [
    "shopify",
    "line-items",
    "looping",
    "google-sheets",
    "inventory"
  ],
  "apps": [
    "Shopify",
    "Code by Zapier",
    "Filter by Zapier",
    "Looping by Zapier",
    "Google Sheets"
  ],
  "plan": {
    "minimum": "Professional",
    "reasons": [
      "multi-step Zap: 4 steps after the trigger",
      "Filter by Zapier",
      "premium app: Shopify"
    ]
  },
  "tasks": {
    "typical": 2,
    "max": 2
  },
  "trigger": {
    "app": "Shopify",
    "event": "New Order",
    "kind": "instant",
    "title": "New order",
    "why": "Fires on every new order, paid or not, because production planning starts at checkout.",
    "config": {
      "Store": "YOUR_STORE"
    },
    "outputs": [
      "id",
      "order_number",
      "email",
      "line_item_titles",
      "line_item_quantities",
      "line_item_prices",
      "line_item_skus",
      "currency",
      "created_at"
    ],
    "sample": "sample-trigger.json"
  },
  "steps": [
    {
      "key": "lines",
      "kind": "code",
      "app": "Code by Zapier",
      "event": "Run JavaScript",
      "title": "Check the line items line up",
      "why": "Counts the comma-joined lists and reports `mismatch` when a title with a comma has broken the join, before any row is written.",
      "code": {
        "language": "javascript",
        "module": "line-items",
        "file": "code/line-items.js"
      },
      "input_data": {
        "titles": "{{trigger.line_item_titles}}",
        "quantities": "{{trigger.line_item_quantities}}",
        "prices": "{{trigger.line_item_prices}}",
        "skus": "{{trigger.line_item_skus}}",
        "currency": "{{trigger.currency}}"
      },
      "outputs": [
        "line_count",
        "unit_count",
        "subtotal",
        "summary",
        "missing_skus",
        "mismatch"
      ]
    },
    {
      "key": "aligned",
      "kind": "filter",
      "app": "Filter by Zapier",
      "event": "Only continue if...",
      "title": "Only orders whose lists line up",
      "why": "A mismatched order stops here instead of writing shifted rows. Fix the product title in Shopify and re-run the order.",
      "conditions": [
        [
          {
            "field": "{{lines.mismatch}}",
            "operator": "(Boolean) Is false"
          }
        ]
      ],
      "usually": true
    },
    {
      "key": "loop",
      "kind": "looping",
      "app": "Looping by Zapier",
      "event": "Create Loop From Line Items",
      "title": "Loop over the line items",
      "why": "Runs the next step once per product. Each key below becomes a field inside the loop.",
      "fields": {
        "Values to Loop": {
          "title": "{{trigger.line_item_titles}}",
          "quantity": "{{trigger.line_item_quantities}}",
          "price": "{{trigger.line_item_prices}}",
          "sku": "{{trigger.line_item_skus}}"
        },
        "Maximum number of loop iterations": "50"
      },
      "outputs": [
        "loop_iteration",
        "loop_iteration_is_last",
        "title",
        "quantity",
        "price",
        "sku"
      ]
    },
    {
      "key": "row",
      "kind": "action",
      "app": "Google Sheets",
      "event": "Create Spreadsheet Row",
      "title": "Add one row for this product",
      "why": "Runs once per line item, so an order with four products writes four rows.",
      "fields": {
        "Drive": "My Google Drive",
        "Spreadsheet": "Production",
        "Worksheet": "Order lines",
        "Columns": {
          "order_number": "{{trigger.order_number}}",
          "ordered_at": "{{trigger.created_at}}",
          "line": "{{loop.loop_iteration}}",
          "sku": "{{loop.sku}}",
          "product": "{{loop.title}}",
          "quantity": "{{loop.quantity}}",
          "unit_price": "{{loop.price}}",
          "order_subtotal": "{{lines.subtotal}}"
        }
      },
      "outputs": [
        "id",
        "row_number"
      ]
    }
  ]
}
