{
  "name": "MZPanel: new WooCommerce order -> Google Sheet",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 5
            }
          ]
        }
      },
      "id": "d5f29894-7e54-5589-904d-ca00019c0d97",
      "name": "Every 5 minutes",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "657f27a0-c0dd-5a11-b2d2-bf162d6674bd",
              "name": "apiBase",
              "value": "https://api.mzpanel.com",
              "type": "string"
            },
            {
              "id": "5579cb69-d07b-55c2-b4da-681974e2702f",
              "name": "serverId",
              "value": "REPLACE-WITH-SERVER-UUID",
              "type": "string"
            },
            {
              "id": "da0c19bc-61d2-5751-a8d2-7ae0cb4718f7",
              "name": "domain",
              "value": "shop.example.com",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "fd71e4d2-ec17-598a-8683-369dc645d199",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{ $('Settings').first().json.apiBase }}/v1/servers/{{ $('Settings').first().json.serverId }}/wc/orders?scope=site&domain={{ $('Settings').first().json.domain }}&per_page=50",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "id": "7dde46f0-99d4-58bb-8a69-608077396c37",
      "name": "Latest orders",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "mzpanel-api-token",
          "name": "MZPanel API token"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Keep only orders newer than the last one seen (workflow static data; the\n// first run records the newest id and appends nothing).\nconst state = $getWorkflowStaticData('global');\nconst body = $input.first().json;\nconst items = body.items || (body.data && body.data.items) || [];\nconst maxId = items.reduce((m, o) => Math.max(m, o.id || 0), 0);\nif (!state.lastOrderId) {\n  state.lastOrderId = maxId;\n  return [];\n}\nconst fresh = items.filter((o) => o.id > state.lastOrderId).sort((a, b) => a.id - b.id);\nif (maxId > state.lastOrderId) state.lastOrderId = maxId;\nreturn fresh.map((o) => ({ json: {\n  order: o.number || o.id, date: o.date_created, status: o.status, total: o.total, currency: o.currency,\n  customer: o.customer_name, email: o.billing_email, payment: o.payment_method_title, items: o.item_count,\n} }));\n"
      },
      "id": "7d4de95f-ba7b-5e6e-8062-c32f0e488244",
      "name": "New orders only",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "https://docs.google.com/spreadsheets/d/REPLACE"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Orders"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        },
        "options": {}
      },
      "id": "55b158c5-d45e-5a7b-a891-599e67f5221a",
      "name": "Append to sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        880,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "google-sheets",
          "name": "Google Sheets account"
        }
      }
    }
  ],
  "connections": {
    "Every 5 minutes": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Latest orders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Latest orders": {
      "main": [
        [
          {
            "node": "New orders only",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "New orders only": {
      "main": [
        [
          {
            "node": "Append to sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "mzpanel": "Polls GET /v1/servers/{id}/wc/orders?scope=site&domain= (Read token) every 5 minutes and appends new orders (order, date, status, total, currency, customer, email, payment, items) to a Google Sheet."
  }
}
