在 WooCommerce 客户完成订单电子邮件通知中添加基于运输方式 ID 的消息

Add message based on shipping method ID in WooCommerce customer completed order email notification

我想在 WooCommerce 客户完成订单电子邮件通知中添加一条基于送货方式 ID 的消息。

我正在使用以下代码:

add_action ('woocommerce_email_order_details', 'custom_email_notification_for_shipping', 5, 4);
function custom_email_notification_for_shipping( $order, $sent_to_admin, $plain_text, $email ){

    // Only for "completed" email notification and "Local Pickup" Shipping Method
    if ( 'customer_completed_order' == $email->id && $order->has_shipping_method('local_pickup') ){
        $order_id = $order->get_id(); // The Order ID

        // Your message output
        echo "<h2>TEST</h2>
        <p>Shipping info test</p>";
    }
}

这可行,但我有 2 个不同的本地取件

是否可以将其设置为仅 local_pickup:10?

要获取送货方式 ID,您可以使用 $shipping_item->get_method_id() + $shipping_item->get_instance_id() 并连接字符串

所以你得到:

function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
    // Target specific email
    if ( $email->id == 'customer_completed_order' ) {
        // Get the shipping method ID
        $shipping_items      = $order->get_items( 'shipping' );
        $shipping_item       = reset( $shipping_items );
        $shipping_method_id  = $shipping_item->get_method_id() . ':';
        $shipping_method_id .= $shipping_item->get_instance_id();
         
        // Compare
        if ( $shipping_method_id == 'local_pickup:1' ) {
            echo 'test 1';
        } elseif ( $shipping_method_id == 'local_pickup:10' ) {
            echo 'test 2';
        } elseif ( $shipping_method_id == 'local_pickup:11' ) {
            echo 'test 3';
        } else {
            echo 'DEBUG INFORMATION = ' . $shipping_method_id;
        }
     }
}
add_action ('woocommerce_email_order_details', 'action_woocommerce_email_order_details', 10, 4 );