根据运输方式自定义 Woocommerce 新订单电子邮件通知 [英] Customizing Woocommerce New Order email notification based on shipping method
问题描述
如何编辑 admin-new-order.php WooCommerce 模板以根据运输方式有条件地发送一些自定义客户详细信息?
How can I edit the admin-new-order.php WooCommerce template to send conditionally some custom customer details based on shipping method?
例如(用于新订单电子邮件通知):
- 如果 Order Shipping 方法是
flat rate
,我想向客户详细信息添加一些自定义字段信息. - 如果订单运输方式不是
flat rate
,我不希望显示自定义字段信息.
或者可能在 function.php 中有一些代码片段?
Or maybe there's some snippet that goes in function.php for this?
推荐答案
您可以使用钩在任何此钩子中的自定义函数(调整钩子优先级):
You can use a custom function hooked in any of this hooks (tuning the hook priority):
- woocommerce_email_order_details
- woocommerce_email_order_meta
- woocommerce_email_customer_details
这是一个钩子代码的例子(而不是覆盖模板):
Here it is an example of hooked code (instead of overriding the template):
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,所以它在客户详细信息之后.
The hook priority here is 15, so it comes after the customer details.
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
此代码已经过测试且有效.
This code is tested and works.
这篇关于根据运输方式自定义 Woocommerce 新订单电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!