You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为WooCommerce自定义订单状态邮件新增占位符?

Solution to Add Custom Placeholders for WooCommerce Order Status Emails

Got it, let's walk through how to add both the {billing_email} and {test_pw} placeholders to your custom order status email notifications. You’ll use WooCommerce’s built-in hooks to register these placeholders and define their replacement values—here’s a step-by-step implementation:

Step 1: Add the Placeholders via Code

Drop this code into your theme’s functions.php file or a custom plugin (using a plugin is safer to avoid losing changes when updating your theme):

add_filter( 'woocommerce_email_placeholders', 'add_custom_order_email_placeholders', 10, 2 );
function add_custom_order_email_placeholders( $placeholders, $email ) {
    // Ensure we're working with a WooCommerce Order object
    if ( isset( $email->object ) && is_a( $email->object, 'WC_Order' ) ) {
        $order = $email->object;

        // Add {billing_email} placeholder
        $placeholders['{billing_email}'] = $order->get_billing_email();

        // Add {test_pw} placeholder (pulled from custom hidden field 'test_pw')
        $test_pw_value = $order->get_meta( 'test_pw', true ); 
        // Use 'N/A' as fallback if the field has no value
        $placeholders['{test_pw}'] = ! empty( $test_pw_value ) ? $test_pw_value : 'N/A';
    }

    return $placeholders;
}

Quick Breakdown of the Code:

  • We use the woocommerce_email_placeholders filter, which lets us inject new placeholders into all WooCommerce order emails.
  • For {billing_email}, we just fetch the existing billing email directly from the order object using $order->get_billing_email().
  • For {test_pw}, we use $order->get_meta() to retrieve the value of your custom hidden field. The true parameter ensures we get a single value (not an array) if the field was saved correctly. We also add a fallback in case the field is empty for some orders.

Step 2: (Optional) Restrict to Your Custom Order Status

If you only want these placeholders to work for your specific custom order status (e.g., custom-processing), add an extra check inside the function:

add_filter( 'woocommerce_email_placeholders', 'add_custom_order_email_placeholders', 10, 2 );
function add_custom_order_email_placeholders( $placeholders, $email ) {
    if ( isset( $email->object ) && is_a( $email->object, 'WC_Order' ) ) {
        $order = $email->object;
        
        // Only apply to your target custom order status
        if ( $order->get_status() === 'custom-processing' ) {
            $placeholders['{billing_email}'] = $order->get_billing_email();
            
            $test_pw_value = $order->get_meta( 'test_pw', true );
            $placeholders['{test_pw}'] = ! empty( $test_pw_value ) ? $test_pw_value : 'N/A';
        }
    }

    return $placeholders;
}

Just replace 'custom-processing' with the slug of your actual custom order status.

Step 3: Test the Placeholders

After adding the code, head to your WooCommerce email settings for the custom order status. Update the email subject (or body) to include {billing_email} and {test_pw}, then trigger a test order with that status to confirm the placeholders are replaced with the correct values.

Note: Make sure your custom hidden field test_pw is being saved to the order meta properly. If you added this field during checkout, you should have code like $order->update_meta_data( 'test_pw', $_POST['test_pw'] ); to save its value to the order.

内容的提问来源于stack exchange,提问作者user1915410

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:06:06