wordpress 在确认电子邮件中获取客户姓名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17819054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get customers name in confirmation email
提问by Frederik Sk?t
I am working with Woocommerce and I am about to make the order confirmation email.
我正在与 Woocommerce 合作,我即将发送订单确认电子邮件。
It want the mail to say:
它希望邮件说:
"Hi [customers name]"
“嗨[客户姓名]”
How do you get Woocommerce to print the customers name?
您如何让 Woocommerce 打印客户姓名?
回答by Kyle
You need the order object, so depending on what hook you are using it should be there. Try something like this:
您需要 order 对象,因此根据您使用的钩子,它应该在那里。尝试这样的事情:
add_action('woocommerce_order_status_completed','my_woo_email');
function my_woo_email($order_id){
$order = new WC_Order( $order_id );
$to = $order->billing_email;
$subject = 'this is my subject';
$message = 'Hi '.$order->billing_first_name.' '.$order->billing_email;$order->billing_last_name.', thanks for the order!';
woocommerce_mail( $to, $subject, $message, $headers = "Content-Type: text/htmlrn", $attachments = "" )
}
This is untested but should get you started
这是未经测试的,但应该让你开始
回答by Dominik Sp?te
As of WooCommerce 3.x the $order
object's properties should not be accessed directly as suggested in the previous answers. The proper way is now:
从 WooCommerce 3.x 开始,$order
不应按照前面的答案中的建议直接访问对象的属性。现在正确的方法是:
echo 'Hi ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
回答by Michael Mizner
Here's my starter example of getting a bunch of the WooCommerce customer info inside a WP_Query
loop:
这是我在WP_Query
循环中获取一堆 WooCommerce 客户信息的入门示例:
$args = array(
'post_status' => 'any',
'post_type' => 'shop_order',
'posts_per_page' => -1,
'order' => 'ASC'
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
$order = new WC_Order( $query->post->ID );
echo $order->billing_address_1;
echo $order->billing_address_2;
echo $order->billing_city;
echo $order->billing_company;
echo $order->billing_country;
echo $order->billing_email;
echo $order->billing_first_name;
echo $order->billing_last_name;
echo $order->billing_phone;
echo $order->billing_postcode;
echo $order->billing_state;
echo $order->cart_discount;
echo $order->cart_discount_tax;
echo $order->customer_ip_address;
echo $order->customer_user;
echo $order->customer_user_agent;
echo $order->order_currency;
echo $order->order_discount;
echo $order->order_key;
echo $order->order_shipping;
echo $order->order_shipping_tax;
echo $order->order_tax;
echo $order->order_total;
echo $order->payment_method;
echo $order->payment_method_title;
echo $order->shipping_address_1;
echo $order->shipping_address_2;
echo $order->shipping_city;
echo $order->shipping_company;
echo $order->shipping_country;
echo $order->shipping_first_name;
echo $order->shipping_last_name;
echo $order->shipping_method_title;
echo $order->shipping_postcode;
echo $order->shipping_state;
}
wp_reset_postdata();
Hopefully this will be helpful for someone else.
希望这对其他人有帮助。
回答by Lafif Astahdziq
you can get customer data from $order
object, something like this.
你可以从$order
对象中获取客户数据,就像这样。
$customer = get_userdata( $order->get_customer_id() );
$name = $customer->display_name;