1
votes

When a new order is created, woocommerce will send an email to admin, and I want it to send customer's city location inside the email as well.

Adding this code in function.php file will only show the IP requiring additional step to open browser and search for IP location to find the city:

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 )
    echo '<br><p><strong>Customer IP address:</strong> '. get_post_meta( $order->id, '_customer_ip_address', true ).'</p>';
} 

There are clients placing fake orders using false address while this will show me the real city location.

So how can I display the customer City?

Code comes from: Display the customer IP Address in new order email notification

1

1 Answers

1
votes

You can add to the customer IP address, his billing city too, this way:

add_action( 'woocommerce_email_customer_details', 'send_customer_city', 10, 4 );
function send_customer_city( $order, $sent_to_admin, $plain_text, $email ){

    // Just for admin new order notification
    if( 'new_order' == $email->id ){
        echo '<br>
        <p><strong>'.__('Customer IP address').':</strong> '. get_post_meta( $order->id, '_customer_ip_address', true ).'</p>
        <p><strong>'.__('Customer city').':</strong> '. get_post_meta( $order->get_id(), '_billing_city', true ).'</p>';
    } 
} 

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

Tested and works