WooCommerce 添加基于付款方式和送货方式的自定义电子邮件内容

WooCommerce add custom email content based on payment method and shipping method

我正在尝试根据付款方式和送货方式的组合向 woocommerce 完成的订单电子邮件通知添加不同的内容。

到目前为止我的代码:

// completed order email instructions

function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {
    if (( get_post_meta($order->id, '_payment_method', true) == 'cod' ) && ( get_post_meta($order->id, '_shipping_method', true) == 'local pickup' )){
    echo "something1";
} 
    elseif (( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) && ( get_post_meta($order->id, '_shipping_method', true) == 'local pickup' )){
    echo "something2";
 }
    else {
    echo "something3";
 }} 

付款部分有效(我得到了 "something1" 到 "something3" 的正确内容)但是如果我添加 && 运输条件,我会得到 "something3" 每种付款方式。

知道哪里出了问题吗?我该如何让它发挥作用?

谢谢

有很多小东西需要改变(例如 post 元支付方式是一个数组):

// (Added this missing hook in your code)
add_action( 'woocommerce_email_order_details', 'my_completed_order_email_instructions', 10, 4 );
function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {

    // Only for "Customer Completed Order" email notification
    if( 'customer_completed_order' != $email->id ) return;

    // Comptibility With WC 3.0+
    if ( method_exists( $order, 'get_id' ) ) {
        $order_id = $order->get_id();
    } else {
        $order_id = $order->id;
    }
    //$order->has_shipping_method('')
    $payment_method = get_post_meta($order_id, '_payment_method', true);
    $shipping_method_arr = get_post_meta($order_id, '_shipping_method', false); // an array
    $method_id = explode( ':', $shipping_method_arr[0][0] );
    $method_id = $method_id[0];  // We get the slug type method


    if ( 'cod' == $payment_method && 'local_pickup' == $method_id ){
        echo "something1";
    } elseif ( 'bacs' == $payment_method && 'local_pickup' == $method_id ){
        echo "something2";
    } else {
        echo "something3";
    }
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

此代码已经过测试并适用于 WooCommerce 版本 2。6.x 和 3+