Display the customer IP Address in new order email notification

£可爱£侵袭症+ 提交于 2019-12-04 18:29:04

(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 bellow, 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!