根据 WooCommerce 客户处理订单电子邮件通知中的付款方式添加自定义消息

Add custom message according to payment method in WooCommerce customer processing order email notification

我正在为电子邮件添加带有钩子的文本,但我希望它根据付款方式进行更改。

我的代码尝试:

add_action( 'woocommerce_email_before_order_table', 'bbloomer_add_content_specific_email', 20, 4 );
  
function bbloomer_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email, $order_id ) {
    $order = wc_get_order( $order_id );
       $order = wc_get_order( $order_id );
    $user_complete_name_and_email = $order->billing_first_name . ' ' . $order->billing_last_name . ' <' . $order->billing_email . '>';
    $to = $user_complete_name_and_email;
   if ( $email->id == 'customer_processing_order' ) {
       if ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) {
           echo '<p>One text</p>';
    }elseif ( get_post_meta($order->id, '_payment_method', true) == 'wocommerce_yape_peru' ) {
           echo '<p>Another text</p>';
    }
       elseif ( get_post_meta($order->id, '_payment_method', true) == 'woo-mercado-pago-custom' ) {
           echo '<p>Third text</p>';
    }
   }
}

很遗憾,这没有达到预期的效果。难道我做错了什么?有什么建议吗?

你可以使用$order->get_payment_method(),所以你得到:

function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {   
    // Only for order processing email 
    if ( $email->id == 'customer_processing_order' ) {
        // Get payment method
        $payment_method = $order->get_payment_method();
        
        // Compare
        if ( $payment_method == 'cod' ) {
            echo 'text 1';
        } elseif( $payment_method == 'bacs' ) {
            echo 'text 2';
        } else {
            echo 'text 3';
        }
    }    
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );