创建新订单时,woocommerce 会向管理员发送一封电子邮件,我希望它也将客户的 IP 地址发送到电子邮件中。但我无法让它工作,这是我到目前为止得到的:
<?php echo get_post_meta( $order->id, '_customer_ip_address', true ); ?>
这段代码进入mytheme/woocommerce/emails/admin-new-order.php
有任何想法吗?
谢谢。
创建新订单时,woocommerce 会向管理员发送一封电子邮件,我希望它也将客户的 IP 地址发送到电子邮件中。但我无法让它工作,这是我到目前为止得到的:
<?php echo get_post_meta( $order->id, '_customer_ip_address', true ); ?>
这段代码进入mytheme/woocommerce/emails/admin-new-order.php
有任何想法吗?
谢谢。
(增加了对 WooCommerce 版本 3+ 的兼容性)
更新 2:添加了一个条件,仅显示管理员新订单通知的地址 IP。
$email_id
替换为未定义$email->id;
您可以对电子邮件通知使用任何相关挂钩,并且不需要覆盖 WooCommerce 电子邮件模板。
在下面的示例中,客户 IP 地址将显示在客户详细信息之前,using woocommerce_email_customer_details
钩子:
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