在 Woocommerce 电子邮件通知中根据送货方式显示自定义内容

Display custom content based on shipping method in Woocommerce email notification

Woocommerce 更新到 3.2 后,下面这段代码不再有效。

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('')
    $shipping_method_arr = get_post_meta($order_id, '_shipping_method', false); // an array
    $rate_id = $shipping_method_arr[0][0]; // the rate ID

    if ( 'flat_rate:10' == $rate_id ){
        echo pll__("Text 1");
    } else {
        echo pll__("Text 2");
    }
}

这段代码有什么错误或过时的地方?
需要进行哪些更改才能使其再次运行?

以下是获取订单中使用的运输方式并使此功能按预期工作的正确方法:

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;

    $found = false; // Initializing variable

    // Iterating through Order shipping methods
    foreach($order->get_shipping_methods() as $value){
        $rate_id = $value->get_method_id(); // Get the shipping rate ID
        if ( 'flat_rate:10' == $rate_id )
            $found = true;
    }

    if ($found)
        echo '<p>'.__("Text 1 (found)","woocommerce").'</p>';
    else
        echo '<p>'.__("Text 2 (else)","woocommerce").'</p>';
}

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

经过测试并有效……