根据运输方式自定义 Woocommerce 新订单电子邮件通知

Customizing Woocommerce New Order email notification based on shipping method

我如何编辑 admin-new-order.php WooCommerce 模板以根据送货方式有条件地发送一些自定义客户详细信息?

例如(用于新订单电子邮件通知):

或者 function.php 中可能有一些片段为此?

您可以使用挂接到任何此挂钩中的自定义函数 (调整挂钩优先级):

  • woocommerce_email_order_details
  • woocommerce_email_order_meta
  • woocommerce_email_customer_details

这是一个钩子代码的例子(而不是覆盖模板):

add_action ('woocommerce_email_customer_details', 'custom_email_customer_details', 15, 4);
function custom_email_customer_details( $order, $sent_to_admin, $plain_text, $email ){

    // Only for "New Order" email notification
    if ( 'new_order' != $email->id ) return;

    // Only "Flat Rate" Shipping Method
    if ( $order->has_shipping_method('flat_rate') ){
        $order_id = $order->get_id(); // The Order ID

        // Test output
        echo "<p>Message: The Order ID is $order_id</p>";
        echo '<p>Custom field: '. get_post_meta( $order_id, '_recorded_sales', true ) .'</p>';
    }
}

这里的挂钩优先级是15,所以它在客户详细信息之后。

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

此代码已经过测试并且有效。