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

创建新订单时,woocommerce将向管理员发送电子邮件,我也希望它在电子邮件内发送客户的IP地址。但我无法使其正常工作,这是到目前为止我得到的:

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

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

有任何想法吗?

谢谢。

参考方案

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

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数

PHP-如何获取类的成员函数列表? - php

如果我知道班级的名字。有没有办法知道类的成员函数列表? 参考方案 get_class_methods()是你的朋友