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

dav*_*nal 2 php wordpress email-notifications woocommerce hook-woocommerce

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

<?php echo get_post_meta( $order->id, '_customer_ip_address', true ); ?>
Run Code Online (Sandbox Code Playgroud)

这段代码进来了 mytheme/woocommerce/emails/admin-new-order.php

有任何想法吗?

谢谢.

Loi*_*tec 5

(增加了WooCommerce版本3+的兼容性)

更新2:添加了一个条件,仅显示管理新订单通知的地址IP.替换为undefined $email_idby$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>';
    }
} 
Run Code Online (Sandbox Code Playgroud)

此代码已经过测试,功能齐全.

代码位于活动子主题(或主题)的function.php文件中.或者也可以在任何插件php文件中.

您也可以使用这些钩子:

woocommerce_email_order_details
woocommerce_email_before_order_table
woocommerce_email_after_order_table
woocommerce_email_order_meta