Back to Community

How to Customize Address Formats in Your WooCommerce PDF Invoices

Content

One of the most common challenges when generating PDF invoices is getting the address formatting just right. Whether you need to display a country name, change a state abbreviation, or reorder address lines, the solution often lies in understanding how the 'PDF Invoices & Packing Slips for WooCommerce' plugin relies on WooCommerce's core functionality.

Why Do Address Formatting Issues Occur?

The plugin uses WooCommerce's built-in functions to format both the shop address and customer addresses. This means the output in your PDF documents is directly tied to your WooCommerce settings and any other plugins or theme code that might be filtering address formats. Common issues include:

  • Countries not displaying when they match the shop's base country.
  • States showing codes (e.g., 'CA') instead of full names (e.g., 'California').
  • Address parts appearing in an unexpected order for your region.
  • Shop address fields, like state, not appearing on the invoice.

Common Solutions for Address Customization

1. Always Display the Country in Addresses

By default, WooCommerce hides the country if it is the same as your shop's base country. To force it to always appear, add this code snippet to your child theme's functions.php file:

add_filter( 'woocommerce_formatted_address_force_country_display', '__return_true' );

2. Change State Abbreviations to Full Names (or Vice Versa)

To modify how states are displayed, you must use the woocommerce_states filter. For example, to show Australian state abbreviations instead of full names, use this code:

add_filter( 'woocommerce_states', function( $states ) {
    $states['AU'] = array(
        'ACT' => 'ACT',
        'NSW' => 'NSW',
        'NT'  => 'NT',
        'QLD' => 'QLD',
        'SA'  => 'SA',
        'TAS' => 'TAS',
        'VIC' => 'VIC',
        'WA'  => 'WA',
    );
    return $states;
}, 10, 1 );

3. Remove the Country from the Shop Address

If you don't want the country to show in your shop's address on the invoice, a simple configuration change often works. Navigate to WooCommerce > PDF Invoices > General tab. In the Shop Information panel, set the Shop Country field to "Select a country" (the first, empty option). You can then enter your full address, including city and postal code, in the Shop City field.

4. Apply Bold or Other Styling to an Address Line

To make a specific part of the address bold, such as the company name, you can use a filter. This snippet will wrap the billing company name in <strong> tags:

add_filter( 'wpo_wcpdf_billing_address', function( $address, $document ){
    if ( $order = $document->order ) {
        if ( $company_name = $order->get_billing_company() ) {
            $address = str_replace( $company_name, "<strong>{$company_name}</strong>", $address );
        }
    }
    return $address;
}, 999, 2 );

Troubleshooting Tips

  • Check for Conflicts: If your address format is not what you expect, temporarily disable other plugins and switch to a default theme like Storefront to see if the issue is caused by a conflict.
  • Clear Test Data: When testing changes, create a new order with complete address data. Orders created manually in the admin without a customer selected may not display addresses correctly.
  • Template Overrides: If you have customized your PDF template files, ensure your customizations are not interfering with the address output functions like $this->billing_address();.

Remember, the 'PDF Invoices & Packing Slips for WooCommerce' plugin mirrors the address data provided by WooCommerce. For complex customizations, such as completely rearranging address lines or adding custom fields, you will likely need to use WooCommerce's extensive filter hooks for address formatting.

Related Support Threads Support