如何在WooCommerce邮件中自定义格式化账单全名(含自定义字段)
Absolutely, you can tweak this to display exactly what you need—your billing first name (the Mr/Mrs title) plus your custom name field! Let’s break down two approaches, including the recommended safe method that won’t break when WooCommerce updates:
1. Quick (but Not Permanent) Core File Edit
If you just need a temporary fix and don’t mind redoing it after WooCommerce updates, you can modify the admin-new-order.php file directly:
- Find the line you mentioned:
<p><?php printf( __( 'You have received an order from %s. The order is as follows:', 'woocommerce' ), $order->get_formatted_billing_full_name() ); ?></p> - Replace it with this code (make sure to swap
_billing_custom_namewith your actual custom field’s meta key):<?php // Grab the title from billing first name $billing_title = $order->get_billing_first_name(); // Grab your custom name field value (update the meta key below!) $custom_name = $order->get_meta('_billing_custom_name'); // Combine them into the full display text $custom_display_name = trim("$billing_title $custom_name"); ?> <p><?php printf( __( 'You have received an order from %s. The order is as follows:', 'woocommerce' ), $custom_display_name ); ?></p>
Note: To find your custom field’s meta key, go to any WooCommerce order edit page, scroll down to the "Custom Fields" section, and look for the "Meta Key" associated with your name field (it usually starts with _billing_ if you added it via checkout fields).
2. Recommended: Use a Filter Hook (Permanent & Safe)
Modifying core files isn’t ideal because updates will overwrite your changes. Instead, use a WooCommerce filter hook to override the get_formatted_billing_full_name() output globally (including in the admin new order email):
- Go to your WordPress dashboard → Appearance → Theme File Editor → Open your theme’s
functions.phpfile (or use a child theme for extra safety). - Add this code at the bottom (again, replace
_billing_custom_namewith your actual meta key):add_filter('woocommerce_order_formatted_billing_full_name', 'custom_admin_email_billing_name', 10, 2); function custom_admin_email_billing_name($formatted_name, $order) { // Get the title from billing first name $billing_title = $order->get_billing_first_name(); // Get your custom name field value $custom_name = $order->get_meta('_billing_custom_name'); // Only override if the custom name exists; fallback to default if not if (!empty($custom_name)) { $formatted_name = trim("$billing_title $custom_name"); } return $formatted_name; } - Save the file, and test a new order—your admin email should now show the title plus your custom name field!
This method is better because it’s tied to your theme (or child theme) and won’t be lost when WooCommerce updates.
内容的提问来源于stack exchange,提问作者Carina

