3

When a new order is created, woocommerce will send an email to admin, and I want it to send customer's IP address inside the email as well. But I can't get it work, here is what I got so far:

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

This code goes in mytheme/woocommerce/emails/admin-new-order.php

Any ideas?

Thanks.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
davidchannal
  • 118
  • 2
  • 8
  • **Where** you tried this is probably more important than **what** you tried. Please explain *exactly where you put this code*. (Also, you won't `echo` in an email, so that's likely part of the problem). – random_user_name Dec 13 '16 at 03:54
  • Hi @cale_b , I've updated my question, the code is in admin-new-order.php. And about the echo, is it right if I change to: `echo __( 'Customer IP Address', 'woocommerce' ) . esc_html( get_post_meta( $order->id, '_customer_ip_address', true ) ) . "\n";` – davidchannal Dec 13 '16 at 04:09

1 Answers1

6

(added compatibility for WooCommerce versions 3+)

Update 2: Added a condition to display the address IP only for Admin New orders notification. Replaced undefined $email_id by $email->id;

You can use any related hooks for the emails notifications and you don't need to override the WooCommerce emails templates.

In the example below, the customer IP address will be displayed just before the customer details, 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>';
    }
} 

This code is tested and is fully functional.

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

You can use also instead these hooks:

woocommerce_email_order_details
woocommerce_email_before_order_table
woocommerce_email_after_order_table
woocommerce_email_order_meta

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi @LoicTheAztec your code works like a charm, thanks! – davidchannal Dec 14 '16 at 09:52
  • Is there anyway to receive the city instead of IP ? I have many false orders but IP shows the truth behind it. – marius Nov 16 '17 at 16:24
  • 1
    @marius Yes you ca use `get_post_meta( $order->get_id(), '_billing_city', true )` intead to get the city … I have updated this answer as it was not compatible with Woocommerce version 3+ – LoicTheAztec Nov 16 '17 at 18:32