在新订单电子邮件通知中显示客户IP地址

时间:2016-12-13 03:42:07

标签: php wordpress woocommerce hook-woocommerce email-notifications

创建新订单时,woocommerce将向管理员发送电子邮件,我希望它也能在电子邮件中发送客户的IP地址。但我无法让它发挥作用,这是我到目前为止所得到的:

<?php echo get_post_meta( $order->id, '_customer_ip_address', true ); ?>

此代码位于mytheme/woocommerce/emails/admin-new-order.php

有什么想法吗?

感谢。

1 个答案:

答案 0 :(得分:5)

(增加了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

相关问题