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

时间:2021-06-22 23:41:58

标签: php wordpress woocommerce email-notifications payment-method

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

我的代码尝试:

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>';
    }
   }
}

不幸的是,这没有预期的结果。难道我做错了什么?有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用 $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 );