Skip to content

WooCommerce Integration

Erstelle automatisch EU-konforme E-Rechnungen fuer jede WooCommerce-Bestellung. WordPress-Webhook → thelawin.dev → ZUGFeRD-PDF.

Voraussetzungen

  • WordPress mit WooCommerce
  • thelawin.dev API Key
  • PHP 8.0+ oder externer Webhook-Empfaenger (n8n/Zapier)

WooCommerce Webhook einrichten

  1. WooCommerce → Settings → Advanced → Webhooks
  2. Add Webhook:
    • Name: thelawin Invoice
    • Status: Active
    • Topic: Order updated
    • Delivery URL: Dein Endpunkt
    • Secret: Ein sicheres Secret

PHP-Handler (direkt in WordPress)

php
// In deinem Theme oder Plugin
add_action('woocommerce_order_status_completed', function($order_id) {
    $order = wc_get_order($order_id);

    $items = array_map(function($item) {
        return [
            'description' => $item->get_name(),
            'quantity' => $item->get_quantity(),
            'unit_price' => (float) $item->get_total() / $item->get_quantity(),
            'vat_rate' => 19,
        ];
    }, $order->get_items());

    $response = wp_remote_post('https://api.thelawin.dev/v1/generate', [
        'headers' => [
            'Content-Type' => 'application/json',
            'X-API-Key' => get_option('thelawin_api_key'),
        ],
        'body' => json_encode([
            'format' => 'zugferd',
            'template' => 'minimal',
            'locale' => 'de',
            'invoice' => [
                'number' => 'RE-' . $order->get_order_number(),
                'date' => $order->get_date_created()->format('Y-m-d'),
                'seller' => [
                    'name' => get_option('woocommerce_store_name', 'Mein Shop'),
                    'country' => 'DE',
                    'vat_id' => get_option('thelawin_vat_id'),
                ],
                'buyer' => [
                    'name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
                    'street' => $order->get_billing_address_1(),
                    'city' => $order->get_billing_city(),
                    'postal_code' => $order->get_billing_postcode(),
                    'country' => $order->get_billing_country(),
                ],
                'items' => $items,
            ],
        ]),
        'timeout' => 30,
    ]);

    if (!is_wp_error($response)) {
        $body = json_decode(wp_remote_retrieve_body($response), true);
        if (isset($body['pdf_base64'])) {
            // PDF speichern oder per Email senden
            $pdf_data = base64_decode($body['pdf_base64']);
            $upload_dir = wp_upload_dir();
            $file_path = $upload_dir['path'] . '/' . $body['filename'];
            file_put_contents($file_path, $pdf_data);

            // Optional: An Bestellungsnotizen anfuegen
            $order->add_order_note('ZUGFeRD-Rechnung erstellt: ' . $body['filename']);
        }
    }
});

Alternative: WooCommerce + n8n

Statt PHP-Code zu schreiben, nutze n8n als Middleware:

  1. WooCommerce Webhook → n8n Webhook Trigger
  2. HTTP Request Node → POST an thelawin.dev
  3. Email Node → PDF an Kunden

Siehe n8n Guide fuer die vollstaendige Anleitung.

Naechste Schritte

ZUGFeRD 2.4 & Factur-X 1.0.8 compliant