如何在WooCommerce邮件及发票PDF的账单/收货地址模板中显示标签
修改WooCommerce邮件及PDF发票的地址显示格式
没问题,我帮你整理两种场景(邮件模板+PDF发票)的修改方法,都是安全且不会被主题/插件更新覆盖的方案:
一、修改邮件模板中的地址格式
方案1:通过子主题覆盖WooCommerce邮件模板
这是最直观的方式,适合喜欢直接编辑模板的同学:
- 找到WooCommerce核心模板文件:
wp-content/plugins/woocommerce/templates/emails/email-addresses.php - 复制这个文件到你的子主题目录下,路径要和核心保持一致:
wp-content/themes/你的子主题/woocommerce/emails/email-addresses.php - 打开复制后的文件,找到账单地址(Billing Address)和收货地址(Shipping Address)的输出代码,替换成自定义格式:
比如原来的账单地址输出段:
<p><strong><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></strong></p> <p class="address"><?php echo $order->get_formatted_billing_address(); ?></p>
改成:
<p><strong><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></strong></p> <p class="address"> <?php // 获取各个地址字段 $billing_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(); $billing_company = $order->get_billing_company(); $billing_address = trim( $order->get_billing_address_1() . ' ' . $order->get_billing_address_2() ); $billing_city_postcode_state = $order->get_billing_city() . ' - ' . $order->get_billing_postcode() . ' ' . $order->get_billing_state(); // 拼接成需要的格式 echo 'Name: ' . esc_html( $billing_name ) . '<br>'; if ( ! empty( $billing_company ) ) { echo 'Company Name: ' . esc_html( $billing_company ) . '<br>'; } echo 'Address: ' . esc_html( $billing_address ) . ' ' . esc_html( $billing_city_postcode_state ); ?> </p>
收货地址的修改逻辑完全一样,把代码里的billing替换成shipping即可。
方案2:通过钩子函数修改(无需编辑模板)
如果你不想碰模板文件,可以在子主题的functions.php里添加以下代码,用WooCommerce的过滤器来重写地址格式:
// 修改账单地址格式 add_filter( 'woocommerce_order_formatted_billing_address', 'custom_billing_address_format', 10, 2 ); function custom_billing_address_format( $address, $order ) { $formatted_lines = []; // 添加姓名行 $formatted_lines[] = 'Name: ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(); // 添加公司行(非空才显示) if ( ! empty( $order->get_billing_company() ) ) { $formatted_lines[] = 'Company Name: ' . $order->get_billing_company(); } // 添加地址行 $address_line = trim( $order->get_billing_address_1() . ' ' . $order->get_billing_address_2() ); $city_postcode_state = $order->get_billing_city() . ' - ' . $order->get_billing_postcode() . ' ' . $order->get_billing_state(); $formatted_lines[] = 'Address: ' . $address_line . ' ' . $city_postcode_state; // 用换行标签拼接所有行 return implode( '<br>', $formatted_lines ); } // 修改收货地址格式 add_filter( 'woocommerce_order_formatted_shipping_address', 'custom_shipping_address_format', 10, 2 ); function custom_shipping_address_format( $address, $order ) { $formatted_lines = []; $formatted_lines[] = 'Name: ' . $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(); if ( ! empty( $order->get_shipping_company() ) ) { $formatted_lines[] = 'Company Name: ' . $order->get_shipping_company(); } $address_line = trim( $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2() ); $city_postcode_state = $order->get_shipping_city() . ' - ' . $order->get_shipping_postcode() . ' ' . $order->get_shipping_state(); $formatted_lines[] = 'Address: ' . $address_line . ' ' . $city_postcode_state; return implode( '<br>', $formatted_lines ); }
二、修改PDF发票中的地址格式
假设你用的是最流行的「WooCommerce PDF Invoices & Packing Slips」插件,提供两种方案:
方案1:覆盖插件模板文件
- 找到插件的默认模板:
wp-content/plugins/woocommerce-pdf-invoices-packing-slips/templates/Simple/invoice.php(如果用的是其他模板风格,替换对应的文件夹名) - 复制到子主题目录:
wp-content/themes/你的子主题/woocommerce/pdf/invoices/invoice.php - 打开文件,找到账单/收货地址的输出代码,替换成自定义格式,比如账单地址部分:
<?php // 自定义账单地址输出 $billing_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(); $billing_company = $order->get_billing_company(); $billing_address = trim( $order->get_billing_address_1() . ' ' . $order->get_billing_address_2() ); $billing_city_postcode_state = $order->get_billing_city() . ' - ' . $order->get_billing_postcode() . ' ' . $order->get_billing_state(); echo 'Name: ' . $billing_name . "\n"; if ( ! empty( $billing_company ) ) { echo 'Company Name: ' . $billing_company . "\n"; } echo 'Address: ' . $billing_address . ' ' . $billing_city_postcode_state; ?>
收货地址同样替换billing为shipping即可,PDF里用\n来换行。
方案2:用插件钩子修改
在子主题的functions.php里添加以下代码,通过插件专属过滤器修改地址格式:
// 修改PDF账单地址 add_filter( 'wpo_wcpdf_billing_address', 'custom_pdf_billing_address', 10, 2 ); function custom_pdf_billing_address( $address, $document ) { $order = $document->order; $formatted_lines = []; $formatted_lines[] = 'Name: ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(); if ( ! empty( $order->get_billing_company() ) ) { $formatted_lines[] = 'Company Name: ' . $order->get_billing_company(); } $address_line = trim( $order->get_billing_address_1() . ' ' . $order->get_billing_address_2() ); $city_postcode_state = $order->get_billing_city() . ' - ' . $order->get_billing_postcode() . ' ' . $order->get_billing_state(); $formatted_lines[] = 'Address: ' . $address_line . ' ' . $city_postcode_state; // PDF用换行符拼接 return implode( "\n", $formatted_lines ); } // 修改PDF收货地址 add_filter( 'wpo_wcpdf_shipping_address', 'custom_pdf_shipping_address', 10, 2 ); function custom_pdf_shipping_address( $address, $document ) { $order = $document->order; $formatted_lines = []; $formatted_lines[] = 'Name: ' . $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(); if ( ! empty( $order->get_shipping_company() ) ) { $formatted_lines[] = 'Company Name: ' . $order->get_shipping_company(); } $address_line = trim( $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2() ); $city_postcode_state = $order->get_shipping_city() . ' - ' . $order->get_shipping_postcode() . ' ' . $order->get_shipping_state(); $formatted_lines[] = 'Address: ' . $address_line . ' ' . $city_postcode_state; return implode( "\n", $formatted_lines ); }
注意事项
- 操作前一定要备份网站(包括文件和数据库),避免出错!
- 所有修改都要在子主题里进行,不要直接修改WooCommerce核心或插件核心文件,否则更新后会丢失自定义内容。
- 如果用的是其他PDF发票插件,钩子或模板路径可能不同,需要对应调整插件的文档。
内容的提问来源于stack exchange,提问作者Ajay Katware
相关产品推荐
相关产品推荐

