电子邮件订单上的Woocommerce用户名

时间:2014-03-11 20:57:17

标签: php wordpress woocommerce

我一直在尝试在订单电子邮件中添加客户的登录名,但到目前为止我刚想通过添加用户ID添加此

<?php do_action('woocommerce_email_after_order_table', $order, false); ?>

<h2><?php _e('Customer details', 'woothemes'); ?></h2>

<?php if ($order->customer_user) : ?>
    <p><strong><?php _e('Usuario:', 'woothemes'); ?></strong> <?php echo $order->customer_user; ?></p>
<?php endif; ?>
<?php if ($order->billing_email) : ?>
    <p><strong><?php _e('Email:', 'woothemes'); ?></strong> <?php echo $order->billing_email; ?></p>
<?php endif; ?>
<?php if ($order->billing_phone) : ?>
    <p><strong><?php _e('Tel:', 'woothemes'); ?></strong> <?php echo $order->billing_phone; ?></p>
<?php endif; ?>

$ order-&gt; customer_user会显示用户的ID,但我要找的是放置用户名,我无法弄清楚我需要使用哪个密钥。

非常感谢任何帮助。

PS;

$ order的转储

object(WC_Order)#7442 (49) { ["id"]=> int(6376) ["status"]=> string(7) "on-hold" ["order_date"]=> string(19) "2014-03-11 22:40:30" ["modified_date"]=> string(19) "2014-03-11 22:40:30" ["customer_note"]=> string(0) "" ["order_custom_fields"]=> array(39) { ["_order_number"]=> array(1) { [0]=> string(4) "6303" } ["_billing_first_name"]=> array(1) { [0]=> string(4) "test" } ["_billing_last_name"]=> array(1) { [0]=> string(4) "test" } ["_billing_company"]=> array(1) { [0]=> string(7) "test123" } ["_billing_address_1"]=> array(1) { [0]=> string(9) "123456789" } ["_billing_address_2"]=> array(1) { [0]=> string(0) "" } ["_billing_postcode"]=> array(1) { [0]=> string(7) "4567894" } ["_billing_city"]=> array(1) { [0]=> string(4) "test" } ["_billing_country"]=> array(1) { [0]=> string(2) "ES" } ["_billing_state"]=> array(1) { [0]=> string(4) "test" } ["_billing_email"]=> array(1) { [0]=> string(19) "xeromidas@gmail.com" } ["_billing_phone"]=> array(1) { [0]=> string(8) "45678965" } ["_shipping_first_name"]=> array(1) { [0]=> string(4) "test" } ["_shipping_last_name"]=> array(1) { [0]=> string(4) "test" } ["_shipping_company"]=> array(1) { [0]=> string(7) "test123" } ["_shipping_address_1"]=> array(1) { [0]=> string(9) "123456789" } ["_shipping_address_2"]=> array(1) { [0]=> string(0) "" } ["_shipping_postcode"]=> array(1) { [0]=> string(7) "4567894" } ["_shipping_city"]=> array(1) { [0]=> string(4) "test" } ["_shipping_country"]=> array(1) { [0]=> string(2) "ES" } ["_shipping_state"]=> array(1) { [0]=> string(4) "test" } ["_shipping_method"]=> array(1) { [0]=> string(13) "free_shipping" } ["_payment_method"]=> array(1) { [0]=> string(6) "cheque" } ["_shipping_method_title"]=> array(1) { [0]=> string(13) "Envío Gratis" } ["_payment_method_title"]=> array(1) { [0]=> string(21) "Pago Contra Reembolso" } ["_order_shipping"]=> array(1) { [0]=> string(4) "0.00" } ["_order_discount"]=> array(1) { [0]=> string(4) "0.00" } ["_cart_discount"]=> array(1) { [0]=> string(4) "0.00" } ["_order_tax"]=> array(1) { [0]=> string(4) "0.00" } ["_order_shipping_tax"]=> array(1) { [0]=> string(4) "0.00" } ["_order_total"]=> array(1) { [0]=> string(5) "55.04" } ["_order_key"]=> array(1) { [0]=> string(19) "order_531f82cfec2bb" } ["_customer_user"]=> array(1) { [0]=> string(3) "759" } ["_order_items"]=> array(1) { [0]=> string(302) "a:1:{i:0;a:10:{s:2:"id";s:4:"5014";s:12:"variation_id";s:0:"";s:4:"name";s:41:"Cuña Queso Viejo de Leche de Oveja 230gr";s:3:"qty";i:8;s:9:"item_meta";a:0:{}s:13:"line_subtotal";s:5:"55.04";s:17:"line_subtotal_tax";s:1:"0";s:10:"line_total";s:5:"55.04";s:8:"line_tax";s:1:"0";s:9:"tax_class";s:0:"";}}" } ["_order_taxes"]=> array(1) { [0]=> string(6) "a:0:{}" } ["_order_currency"]=> array(1) { [0]=> string(3) "EUR" } ["_prices_include_tax"]=> array(1) { [0]=> string(3) "yes" } ["Customer IP Address"]=> array(1) { [0]=> string(13) "200.112.93.90" } ["Customer UA"]=> array(1) { [0]=> string(109) "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36" } } ["order_key"]=> string(19) "order_531f82cfec2bb" ["billing_first_name"]=> string(4) "test" ["billing_last_name"]=> string(4) "test" ["billing_company"]=> string(7) "test123" ["billing_address_1"]=> string(9) "123456789" ["billing_address_2"]=> string(0) "" ["billing_city"]=> string(4) "test" ["billing_postcode"]=> string(7) "4567894" ["billing_country"]=> string(2) "ES" ["billing_state"]=> string(4) "test" ["billing_email"]=> string(19) "xeromidas@gmail.com" ["billing_phone"]=> string(8) "45678965" ["shipping_first_name"]=> string(4) "test" ["shipping_last_name"]=> string(4) "test" ["shipping_company"]=> string(7) "test123" ["shipping_address_1"]=> string(9) "123456789" ["shipping_address_2"]=> string(0) "" ["shipping_city"]=> string(4) "test" ["shipping_postcode"]=> string(7) "4567894" ["shipping_country"]=> string(2) "ES" ["shipping_state"]=> string(4) "test" ["shipping_method"]=> string(13) "free_shipping" ["shipping_method_title"]=> string(13) "Envío Gratis" ["payment_method"]=> string(6) "cheque" ["payment_method_title"]=> string(21) "Pago Contra Reembolso" ["order_discount"]=> string(4) "0.00" ["cart_discount"]=> string(4) "0.00" ["order_tax"]=> string(4) "0.00" ["order_shipping"]=> string(4) "0.00" ["order_shipping_tax"]=> string(4) "0.00" ["order_total"]=> string(5) "55.04" ["items"]=> array(1) { [0]=> array(10) { ["id"]=> string(4) "5014" ["variation_id"]=> string(0) "" ["name"]=> string(41) "Cuña Queso Viejo de Leche de Oveja 230gr" ["qty"]=> int(8) ["item_meta"]=> array(0) { } ["line_subtotal"]=> string(5) "55.04" ["line_subtotal_tax"]=> string(1) "0" ["line_total"]=> string(5) "55.04" ["line_tax"]=> string(1) "0" ["tax_class"]=> string(0) "" } } ["taxes"]=> NULL ["customer_user"]=> string(3) "759" ["user_id"]=> int(759) ["completed_date"]=> string(19) "2014-03-11 22:40:30" ["billing_address"]=> NULL ["formatted_billing_address"]=> string(69) "test test
test123
123456789
4567894 test
test" ["shipping_address"]=> NULL ["formatted_shipping_address"]=> string(69) "test test
test123
123456789
4567894 test
test" ["prices_include_tax"]=> bool(true) ["display_totals_ex_tax"]=> bool(true) ["display_cart_ex_tax"]=> bool(true) }

3 个答案:

答案 0 :(得分:1)

我认为您可以使用get_userdata()从用户的ID中检索用户名。我相信用户ID存储为密钥下的订单的post meta:_customer_user并且根据抽象Order类的__get()函数,我们可以通过$order->user_id访问它。

总而言之,这样的事情可能适用于您的电子邮件模板:

if ( $order->user_id != '' ){
    $user_info = get_userdata($order->user_id);
    echo 'Username: ' . $user_info->user_login . "\n";  
}

答案 1 :(得分:1)

在2018年可以这样完成:

$user = $order->get_user();
$username = $user ? $user->__get('user_login') : '';

不应直接访问属性。 WC_Order-> get_user()返回WP_User或false(对于访客),请参见https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html

WP_User提供了一种神奇的方法__get()来检索用户名(即user_login),请参见https://codex.wordpress.org/Class_Reference/WP_User

答案 2 :(得分:-1)

我们可以按如下方式编辑woo-commerce的模板,

<?php if ( is_user_logged_in()) {

     global $current_user;
     get_currentuserinfo();
 ?>   
    <p><strong><?php _e( 'Customer Log in name:', 'woocommerce' ); ?></strong> <?php echo  $current_user->display_name; ?></p>
<?php } ?>

This can be added after,

<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>

这将允许在电子邮件模板中添加“记录的客户名称”。