14

我正在与 Woocommerce 合作,我即将发送订单确认电子邮件。

它希望邮件说:

“嗨 [客户姓名]”

你如何让 Woocommerce 打印客户姓名?

4

4 回答 4

18

您需要 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 = "" )

}

这是未经测试的,但应该让你开始

于 2013-07-25T15:13:41.847 回答
11

从 WooCommerce 3.x 开始,$order不应按照前面的答案中的建议直接访问对象的属性。现在正确的方法是:

echo 'Hi ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
于 2017-06-08T15:07:16.567 回答
5

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();

希望这对其他人有帮助。

于 2016-09-06T13:40:18.090 回答
0

你可以从$order对象中获取客户数据,就像这样。

$customer = get_userdata( $order->get_customer_id() );
$name = $customer->display_name;
于 2016-06-22T07:45:10.517 回答