Back to Community

How to Add a Customer VAT Number (NIF/CIF) to Your WooCommerce PDF Invoices

Content

Adding a customer's VAT number (often called NIF, CIF, or NIE in Spain) to your PDF invoices is a common requirement for WooCommerce store owners. However, users often encounter a specific issue: the VAT number appears on frontend orders but is missing from admin-created orders. This guide explains why this happens and provides the most common solutions.

Why This Problem Occurs

The core issue typically stems from how the custom field data is retrieved and displayed. The 'PDF Invoices & Packing Slips for WooCommerce' plugin uses a specific filter hook to format the billing address on the PDF. If your custom code doesn't correctly target this hook or fetch the order meta data, the VAT number may not appear consistently across all order types.

Most Common Solution: Using a Code Snippet

The most reliable method is to use a small code snippet that hooks into the plugin's billing address filter. This approach ensures the VAT number is displayed in the correct location on the invoice, regardless of how the order was created.

Step 1: Identify the correct meta key for your VAT number field. This is the unique identifier used to store the value in the order's metadata. Common keys include nif, vat_number, _billing_nif, or nif_number. You can find this key by checking your custom checkout field code or a plugin's documentation.

Step 2: Add the following code to your child theme's functions.php file or via a code snippets plugin. Remember to replace 'nif_number' with your actual meta key.

/**
 * Add VAT Number (NIF/CIF) to WooCommerce PDF Invoices
 */
add_filter( 'wpo_wcpdf_billing_address', 'wpo_wcpdf_custom_pdf_billing_address', 10, 2 );
function wpo_wcpdf_custom_pdf_billing_address( $address, $document ) {
    if ( ! $document || ! $document->order ) {
        return $address;
    }

    $order = $document->order;
    // REPLACE 'nif_number' WITH YOUR CUSTOM META KEY
    $meta_key = 'nif_number';
    $vat_number = $order->get_meta( $meta_key ) ?: $order->get_meta( "_$meta_key" );

    if ( ! empty( $vat_number ) ) {
        $vat_label = apply_filters( 'wpo_wcpdf_vat_number_label', 'NIF/CIF: ' );
        $new_line = "<br>" . $vat_label . $vat_number;
        $address .= $new_line;
    }

    return $address;
}

Troubleshooting Common Errors

  • Fatal Error (Call to a member function get_meta() on null): This error, as seen in Thread 6, often occurs when custom code is placed directly inside a template file without proper checks for the order object. Always use the provided filter hooks (wpo_wcpdf_billing_address) instead of modifying template files directly, as they handle the order object correctly.
  • Field Still Not Showing: Double-check that the meta key in the code snippet matches exactly the key used by your checkout field or plugin. The key is case-sensitive.
  • Data Not Saving for Admin Orders: If you create orders in the WordPress admin, ensure your custom field code also captures and saves the VAT number for orders created there, not just on the frontend checkout.

By using the correct filter and meta key, you can reliably display your customers' VAT numbers on all PDF invoices. For more complex customizations involving the invoice totals or layout, the plugin's official documentation is the best resource for advanced template modifications.