Skip to content

WooCommerce Integration

Hook into WooCommerce's order lifecycle and call thelawin.dev to produce a ZUGFeRD PDF for each completed order.

Prerequisites

  • WordPress with WooCommerce
  • thelawin.dev API key
  • PHP 8.0+ or an external webhook receiver (n8n/Zapier)

Set Up the WooCommerce Webhook

  1. WooCommerce → Settings → Advanced → Webhooks
  2. Add Webhook:
    • Name: thelawin Invoice
    • Status: Active
    • Topic: Order updated
    • Delivery URL: Your endpoint
    • Secret: A secure secret string

PHP Handler (directly in WordPress)

php
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', 'My 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_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);

            $order->add_order_note('ZUGFeRD invoice created: ' . $body['filename']);
        }
    }
});

Alternative: WooCommerce + n8n

Instead of writing PHP code, use n8n as middleware:

  1. WooCommerce Webhook → n8n Webhook Trigger
  2. HTTP Request Node → POST to thelawin.dev
  3. Email Node → Send PDF to customer

See the n8n Guide for a full walkthrough.

Next Steps

ZUGFeRD 2.4 & Factur-X 1.0.8 compliant