(增加了对 WooCommerce 版本 3+ 的兼容性)
更新2:添加了仅在管理员新订单通知时显示地址 IP 的条件。替换未定义$email_id
by $email->id;
您可以使用任何相关的电子邮件通知挂钩,并且无需覆盖 WooCommerce 电子邮件模板。
在下面的示例中,客户 IP 地址将显示在客户详细信息之前,using woocommerce_email_customer_details
hook:
add_action('woocommerce_email_customer_details', 'send_customer_ip_adress', 10, 4);
function send_customer_ip_adress($order, $sent_to_admin, $plain_text, $email){
// Just for admin new order notification
if( 'new_order' == $email->id ){
// WC3+ compatibility
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
echo '<br><p><strong>Customer IP address:</strong> '. get_post_meta( $order_id, '_customer_ip_address', true ).'</p>';
}
}
该代码已经过测试并且功能齐全。
代码位于活动子主题(或主题)的 function.php 文件中。或者也可以在任何插件 php 文件中。
您也可以使用这些钩子:
woocommerce_email_order_details
woocommerce_email_before_order_table
woocommerce_email_after_order_table
woocommerce_email_order_meta