{
  "name": "Convert WooCommerce orders to XRechnung email attachments",
  "nodes": [
    {
      "parameters": {
        "event": "order.created"
      },
      "id": "a91a8ebf-a3dc-4ae2-9fa1-da3ca1d37414",
      "name": "WooCommerce Trigger",
      "type": "n8n-nodes-base.wooCommerceTrigger",
      "typeVersion": 1,
      "position": [
        16,
        480
      ],
      "webhookId": "1a0acb94-cd0a-475d-8656-40619147df54",
      "credentials": {
        "wooCommerceApi": {
          "id": "",
          "name": "WooCommerce account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "cfg-seller-name",
              "name": "sellerName",
              "value": "",
              "type": "string"
            },
            {
              "id": "cfg-seller-vat",
              "name": "sellerVatId",
              "value": "",
              "type": "string"
            },
            {
              "id": "cfg-seller-email",
              "name": "sellerEmail",
              "value": "",
              "type": "string"
            },
            {
              "id": "cfg-seller-street",
              "name": "sellerStreet",
              "value": "",
              "type": "string"
            },
            {
              "id": "cfg-seller-city",
              "name": "sellerCity",
              "value": "",
              "type": "string"
            },
            {
              "id": "cfg-seller-postal",
              "name": "sellerPostalCode",
              "value": "",
              "type": "string"
            },
            {
              "id": "cfg-seller-country",
              "name": "sellerCountry",
              "value": "DE",
              "type": "string"
            },
            {
              "id": "cfg-vat-rate",
              "name": "vatRate",
              "value": 19,
              "type": "number"
            },
            {
              "id": "cfg-country",
              "name": "countryCode",
              "value": "DE",
              "type": "string"
            },
            {
              "id": "cfg-format",
              "name": "format",
              "value": "xrechnung",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "bfa05cf5-5505-4ab5-9f05-05a505a505a5",
      "name": "Set Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        256,
        480
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const order = $json;\nconst cfg = $('Set Config').first().json;\n\n// Map WooCommerce line items\nconst lineItems = (order.line_items || []).map(item => ({\n  description: item.name || '',\n  quantity: item.quantity || 1,\n  unitPrice: parseFloat(item.price || 0),\n  vatRate: cfg.vatRate,\n  unit: 'EA'\n}));\n\nconst subtotal = parseFloat(order.total || 0) - parseFloat(order.total_tax || 0);\nconst totalVat = parseFloat(order.total_tax || 0);\nconst total = parseFloat(order.total || 0);\n\nconst invoice = {\n  invoiceNumber: `WOO-${order.id || order.number || Date.now()}`,\n  invoiceDate: new Date().toISOString().split('T')[0],\n  dueDate: '',\n  currency: order.currency || 'EUR',\n  seller: {\n    name: cfg.sellerName,\n    vatId: cfg.sellerVatId,\n    email: cfg.sellerEmail,\n    address: {\n      street: cfg.sellerStreet,\n      city: cfg.sellerCity,\n      postalCode: cfg.sellerPostalCode,\n      country: cfg.sellerCountry\n    }\n  },\n  buyer: {\n    name: `${order.billing?.first_name || ''} ${order.billing?.last_name || ''}`.trim() || order.billing?.company || '',\n    vatId: order.meta_data?.find(m => m.key === 'vat_id')?.value || '',\n    email: order.billing?.email || '',\n    address: {\n      street: `${order.billing?.address_1 || ''} ${order.billing?.address_2 || ''}`.trim(),\n      city: order.billing?.city || '',\n      postalCode: order.billing?.postcode || '',\n      country: order.billing?.country || 'DE'\n    }\n  },\n  lineItems,\n  subtotal,\n  totalVat,\n  total\n};\n\nreturn {\n  json: {\n    orderId: order.id || order.number,\n    buyerEmail: order.billing?.email || '',\n    invoiceNumber: invoice.invoiceNumber,\n    invoiceData: JSON.stringify(invoice)\n  }\n};"
      },
      "id": "bc544ce9-2cee-4e8a-a310-50d024d4e721",
      "name": "Map Order to Invoice",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        496,
        480
      ]
    },
    {
      "parameters": {
        "countryCode": {
          "__rl": true,
          "value": "={{ $('Set Config').first().json.countryCode }}",
          "mode": "expression"
        },
        "format": {
          "__rl": true,
          "value": "={{ $('Set Config').first().json.format }}",
          "mode": "expression"
        },
        "invoiceData": "={{ $json.invoiceData }}",
        "options": {}
      },
      "id": "d8fdbfa3-c0cd-476c-88ae-2e3f1a5d8795",
      "name": "Generate XRechnung",
      "type": "n8n-nodes-invoice-api-xhub.invoiceXhub",
      "typeVersion": 1,
      "position": [
        736,
        480
      ],
      "credentials": {
        "invoiceXhubApi": {
          "id": "",
          "name": "Invoice-api.xhub Account"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "check-success",
              "leftValue": "={{ $json.success }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "911aa1fb-805a-4323-81ab-973c2ad8a5bf",
      "name": "Generated OK?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        976,
        480
      ]
    },
    {
      "parameters": {
        "sendTo": "={{ $('Map Order to Invoice').item.json.buyerEmail }}",
        "subject": "=Ihre Rechnung {{ $('Map Order to Invoice').item.json.invoiceNumber }}",
        "message": "=Sehr geehrte Damen und Herren,\n\nvielen Dank fuer Ihre Bestellung.\n\nAnbei erhalten Sie Ihre XRechnung {{ $('Map Order to Invoice').item.json.invoiceNumber }}.\n\nMit freundlichen Gruessen",
        "options": {
          "attachmentsUi": {
            "attachmentsBinary": [
              {}
            ]
          }
        }
      },
      "id": "246a99a7-e293-4862-8817-9c4976ef98d3",
      "name": "Gmail: Send Invoice",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        1216,
        384
      ],
      "webhookId": "ff6fce2d-9de5-4ecb-8b96-006c15f93252",
      "credentials": {
        "gmailOAuth2": {
          "id": "",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "resource": "order",
        "operation": "update",
        "orderId": "={{ $('Map Order to Invoice').item.json.orderId }}",
        "updateFields": {
          "customerNote": "=XRechnung {{ $('Map Order to Invoice').item.json.invoiceNumber }} generated and sent."
        },
        "billingUi": {},
        "couponLinesUi": {},
        "feeLinesUi": {},
        "lineItemsUi": {},
        "metadataUi": {},
        "shippingUi": {},
        "shippingLinesUi": {}
      },
      "id": "c3d88e22-3921-4b07-a048-a071393f2fc9",
      "name": "WooCommerce: Add Note",
      "type": "n8n-nodes-base.wooCommerce",
      "typeVersion": 1,
      "position": [
        1456,
        384
      ],
      "credentials": {
        "wooCommerceApi": {
          "id": "",
          "name": "WooCommerce account"
        }
      }
    },
    {
      "parameters": {},
      "id": "84168a03-561c-4a3c-a30a-3337cead7f4e",
      "name": "Generation Error",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1216,
        608
      ]
    },
    {
      "parameters": {
        "content": "## WooCommerce order → XRechnung → buyer mail\n\nEvery new order fires this: map the order → `invoice-api.xhub` generates the XRechnung XML → Gmail emails it to the billing address → the WooCommerce order gets an internal confirmation note.\n\n**One-time config** in `Set Config`: your company name, VAT ID, invoicing email, street, city, postcode, country, default `vatRate`. The mapper reads everything else from the order itself.\n\n**Caveat:** single VAT rate per order. Mixed-rate catalogues (7% + 19%) need a per-line-item tweak in the mapper — not hard, just not shipped.",
        "height": 340,
        "width": 1320,
        "color": 1
      },
      "id": "f2a3b4c5-d6e7-48f9-a0b1-c2d3e4f5a6b7",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -48,
        -448
      ]
    },
    {
      "parameters": {
        "content": "## 1. Map Order Data\nTrigger on a new WooCommerce order and map it to the invoice schema.",
        "height": 230,
        "width": 696
      },
      "id": "494d9da7-4201-4944-9e20-3ad84641e1f6",
      "name": "Section: Map Order",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -48,
        400
      ]
    },
    {
      "parameters": {
        "content": "## 2. Generate XRechnung & Send\nGenerate EN 16931-compliant XML and email it to the buyer.",
        "height": 434,
        "width": 664
      },
      "id": "ee9711eb-e7c6-4446-8384-5f9e7a5d4480",
      "name": "Section: Generate & Send",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        688,
        320
      ]
    },
    {
      "parameters": {
        "content": "## 3. Confirm Order\nAdd a confirmation note to the WooCommerce order.",
        "height": 280,
        "width": 356
      },
      "id": "f5575e09-212f-4385-851f-c8f9e028567a",
      "name": "Section: Confirm",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1392,
        288
      ]
    },
    {
      "parameters": {
        "content": "⚠️ **Configure here:** Replace seller name, VAT ID, email, and address with your real company data. Adjust `vatRate` (default 19%) if needed.",
        "height": 100,
        "width": 320,
        "color": 3
      },
      "id": "warn-setconfig-05",
      "name": "Warning: Configure Set Config",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        272
      ]
    }
  ],
  "connections": {
    "WooCommerce Trigger": {
      "main": [
        [
          {
            "node": "Set Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Map Order to Invoice": {
      "main": [
        [
          {
            "node": "Generate XRechnung",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate XRechnung": {
      "main": [
        [
          {
            "node": "Generated OK?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generated OK?": {
      "main": [
        [
          {
            "node": "Gmail: Send Invoice",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Generation Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail: Send Invoice": {
      "main": [
        [
          {
            "node": "WooCommerce: Add Note",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Config": {
      "main": [
        [
          {
            "node": "Map Order to Invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  }
}