簡體   English   中英

電子郵件訂單上的Woocommerce用戶名

[英]Woocommerce username on email order

我一直在嘗試在訂單電子郵件中添加客戶的登錄名,但是到目前為止,我只是想添加用戶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-> customer_user調出了用戶的ID,但是我正在尋找的是放置用戶名,但我無法弄清楚我需要使用什么密鑰。

任何幫助將非常感激。

PS;

$ order的Var轉儲

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) }

我認為您可以使用get_userdata()從用戶ID中檢索用戶名。 我相信用戶ID在_customer_user項下存儲為訂單的post meta,根據抽象Order類的__get()函數,我們可以通過$order->user_id訪問它。

因此,總的來說,這樣的事情可能在您的電子郵件模板中起作用:

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

在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

我們可以如下編輯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>

這將允許在電子郵件模板中添加已記錄的客戶名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM