我正在与Woocommerce合作,我即将发出订单确认电子邮件。
希望邮件说:
“嗨[客户名称]”
如何让Woocommerce打印客户名称?
答案 0 :(得分:17)
您需要订单对象,因此根据您使用的挂钩,它应该在那里。尝试这样的事情:
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 = "" )
}
这是未经测试但应该让你开始
答案 1 :(得分:9)
从WooCommerce 3.x开始,不应按照前面的答案中的建议直接访问$order
对象的属性。现在正确的方法是:
echo 'Hi ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
答案 2 :(得分:4)
这是我在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();
希望这对其他人有帮助。
答案 3 :(得分:0)
您可以从$order
对象获取客户数据,如下所示。
$customer = get_userdata( $order->get_customer_id() );
$name = $customer->display_name;