如何从WooCommerce中的订单获取客户详细信息?

Alf*_*rez 23 php wordpress woocommerce

我有一个功能,这样做:

$order = new WC_Order($order_id);
$customer = new WC_Customer( $order_id );
Run Code Online (Sandbox Code Playgroud)

如何从中获取客户详细信息?我已经尝试了文档中的所有内容,但不知何故,只是存在一些细节,但其余的不是,例如

$data['Address'] = $customer->get_address() . ' ' . $customer->get_address_2();
$data['ZipCode'] = $customer->get_postcode();
Run Code Online (Sandbox Code Playgroud)

是空的.

干

var_dump($customer)
Run Code Online (Sandbox Code Playgroud)

生产:

object(WC_Customer)#654(2){["_ data":protected] => array(14){["country"] => string(2)"IT"> ["state"] => string(0) ""["postcode"] => string(0)""["city"] => string(0)""["address"] = >> string(0)""["address_2"] => string (0)""["shipping_country"] => string(2)"IT"["shipping_state"] => string(2)"BG"["shipping_postcode"] => string(0)""["shipping_city" ] = >> string(0)""["shipping_address"] => string(0)""["shipping_address_2"] => string(0)""["is_vat_exempt"] => bool(false)["calculated_shipping "] => bool(false)}?["_changed":"WC_Customer":private] => bool(false)}

正如你所看到的,这个城市是现在的,但其余的都是空的.我已经检查了客户的WP_usermeta和管理面板,所有数据都在那里.

有什么想法吗?

Loi*_*tec 35

2017-2020 WooCommerce 版本 3+ 和CRUD 对象

1)。您可以在对象实例上使用 getter 方法WC_Order和WC_Abstract_Order类,WC_Order例如:

// Get an instance of the WC_Order Object from the Order ID (if required)
$order = wc_get_order( $order_id );

// Get the Customer ID (User ID)
$customer_id = $order->get_customer_id(); // Or $order->get_user_id();

// Get the WP_User Object instance
$user = $order->get_user();

// Get the WP_User roles and capabilities
$user_roles = $user->roles;

// Get the Customer billing email
$billing_email  = $order->get_billing_email();

// Get the Customer billing phone
$billing_phone  = $order->get_billing_phone();

// Customer billing information details
$billing_first_name = $order->get_billing_first_name();
$billing_last_name  = $order->get_billing_last_name();
$billing_company    = $order->get_billing_company();
$billing_address_1  = $order->get_billing_address_1();
$billing_address_2  = $order->get_billing_address_2();
$billing_city       = $order->get_billing_city();
$billing_state      = $order->get_billing_state();
$billing_postcode   = $order->get_billing_postcode();
$billing_country    = $order->get_billing_country();

// Customer shipping information details
$shipping_first_name = $order->get_shipping_first_name();
$shipping_last_name  = $order->get_shipping_last_name();
$shipping_company    = $order->get_shipping_company();
$shipping_address_1  = $order->get_shipping_address_1();
$shipping_address_2  = $order->get_shipping_address_2();
$shipping_city       = $order->get_shipping_city();
$shipping_state      = $order->get_shipping_state();
$shipping_postcode   = $order->get_shipping_postcode();
$shipping_country    = $order->get_shipping_country();
Run Code Online (Sandbox Code Playgroud)

2)。您还可以使用该WC_Order get_data()方法从 Order 元数据中获取不受保护的数据数组,例如:

// Get an instance of the WC_Order Object from the Order ID (if required)
$order = wc_get_order( $order_id );

// Get the Order meta data in an unprotected array
$data  = $order->get_data(); // The Order data

$order_id        = $data['id'];
$order_parent_id = $data['parent_id'];

// Get the Customer ID (User ID)
$customer_id     = $data['customer_id'];

## BILLING INFORMATION:

$billing_email      = $data['billing']['email'];
$billing_phone      = $order_data['billing']['phone'];

$billing_first_name = $data['billing']['first_name'];
$billing_last_name  = $data['billing']['last_name'];
$billing_company    = $data['billing']['company'];
$billing_address_1  = $data['billing']['address_1'];
$billing_address_2  = $data['billing']['address_2'];
$billing_city       = $data['billing']['city'];
$billing_state      = $data['billing']['state'];
$billing_postcode   = $data['billing']['postcode'];
$billing_country    = $data['billing']['country'];

## SHIPPING INFORMATION:

$shipping_first_name = $data['shipping']['first_name'];
$shipping_last_name  = $data['shipping']['last_name'];
$shipping_company    = $data['shipping']['company'];
$shipping_address_1  = $data['shipping']['address_1'];
$shipping_address_2  = $data['shipping']['address_2'];
$shipping_city       = $data['shipping']['city'];
$shipping_state      = $data['shipping']['state'];
$shipping_postcode   = $data['shipping']['postcode'];
$shipping_country    = $data['shipping']['country'];
Run Code Online (Sandbox Code Playgroud)

现在获取用户帐户数据(来自订单 ID):

1)。您可以使用WC_Customer类中的方法:

// Get the user ID from an Order ID
$user_id = get_post_meta( $order_id, '_customer_user', true );

// Get an instance of the WC_Customer Object from the user ID
$customer = new WC_Customer( $user_id );

$username     = $customer->get_username(); // Get username
$user_email   = $customer->get_email(); // Get account email
$first_name   = $customer->get_first_name();
$last_name    = $customer->get_last_name();
$display_name = $customer->get_display_name();

// Customer billing information details (from account)
$billing_first_name = $customer->get_billing_first_name();
$billing_last_name  = $customer->get_billing_last_name();
$billing_company    = $customer->get_billing_company();
$billing_address_1  = $customer->get_billing_address_1();
$billing_address_2  = $customer->get_billing_address_2();
$billing_city       = $customer->get_billing_city();
$billing_state      = $customer->get_billing_state();
$billing_postcode   = $customer->get_billing_postcode();
$billing_country    = $customer->get_billing_country();

// Customer shipping information details (from account)
$shipping_first_name = $customer->get_shipping_first_name();
$shipping_last_name  = $customer->get_shipping_last_name();
$shipping_company    = $customer->get_shipping_company();
$shipping_address_1  = $customer->get_shipping_address_1();
$shipping_address_2  = $customer->get_shipping_address_2();
$shipping_city       = $customer->get_shipping_city();
$shipping_state      = $customer->get_shipping_state();
$shipping_postcode   = $customer->get_shipping_postcode();
$shipping_country    = $customer->get_shipping_country();
Run Code Online (Sandbox Code Playgroud)

2)。的WP_User对象(WordPress的):

// Get the user ID from an Order ID
$user_id = get_post_meta( $order_id, '_customer_user', true );

// Get the WP_User instance Object
$user = new WP_User( $user_id );

$username     = $user->username; // Get username
$user_email   = $user->email; // Get account email
$first_name   = $user->first_name;
$last_name    = $user->last_name;
$display_name = $user->display_name;

// Customer billing information details (from account)
$billing_first_name = $user->billing_first_name;
$billing_last_name  = $user->billing_last_name;
$billing_company    = $user->billing_company;
$billing_address_1  = $user->billing_address_1;
$billing_address_2  = $user->billing_address_2;
$billing_city       = $user->billing_city;
$billing_state      = $user->billing_state;
$billing_postcode   = $user->billing_postcode;
$billing_country    = $user->billing_country;

// Customer shipping information details (from account)
$shipping_first_name = $user->shipping_first_name;
$shipping_last_name  = $user->shipping_last_name;
$shipping_company    = $user->shipping_company;
$shipping_address_1  = $user->shipping_address_1;
$shipping_address_2  = $user->shipping_address_2;
$shipping_city       = $user->shipping_city;
$shipping_state      = $user->shipping_state;
$shipping_postcode   = $user->shipping_postcode;
$shipping_country    = $user->shipping_country;
Run Code Online (Sandbox Code Playgroud)

相关:如何获取 WooCommerce 订单详细信息


Rat*_*pps 29

如果您想要客户在订购时输入的客户详细信息,那么您可以使用以下代码

$order = new WC_Order($order_id);
$billing_address = $order->get_billing_address();
$billing_address_html = $order->get_formatted_billing_address(); // for printing or displaying on web page
$shipping_address = $order->get_shipping_address();
$shipping_address_html = $order->get_formatted_shipping_address(); // for printing or displaying on web page
Run Code Online (Sandbox Code Playgroud)

除此之外,$customer = new WC_Customer( $order_id );无法为您提供客户详细信息

首先,new WC_Customer()不采取任何论据

其次,WC_Customer只有当用户登录并且他/她不在管理员端时才会获得客户的详细信息,而不是他/她应该在网站的前端,如"我的帐户","购物","购物车","结帐" '页面

希望这些信息有用.

  • 您可以使用此代码从订单ID获取用户ID $ user_id = get_post_meta($ order_id,'_ customer_user',true); (6认同)
  • $ order-> get_billing_address()不是WC_Order上的方法. (3认同)

ban*_*ing 22

尝试过$customer = new WC_Customer();,global $woocommerce; $customer = $woocommerce->customer;即使我以非管理员用户身份登录,我仍然获得空地址数据.

我的解决方案如下:

function mwe_get_formatted_shipping_name_and_address($user_id) {

    $address = '';
    $address .= get_user_meta( $user_id, 'shipping_first_name', true );
    $address .= ' ';
    $address .= get_user_meta( $user_id, 'shipping_last_name', true );
    $address .= "\n";
    $address .= get_user_meta( $user_id, 'shipping_company', true );
    $address .= "\n";
    $address .= get_user_meta( $user_id, 'shipping_address_1', true );
    $address .= "\n";
    $address .= get_user_meta( $user_id, 'shipping_address_2', true );
    $address .= "\n";
    $address .= get_user_meta( $user_id, 'shipping_city', true );
    $address .= "\n";
    $address .= get_user_meta( $user_id, 'shipping_state', true );
    $address .= "\n";
    $address .= get_user_meta( $user_id, 'shipping_postcode', true );
    $address .= "\n";
    $address .= get_user_meta( $user_id, 'shipping_country', true );

    return $address;
}
Run Code Online (Sandbox Code Playgroud)

...无论您是否以管理员身份登录,此代码都能正常运行.


Col*_*una 16

也许看看Woocomerce Order课程?例如,要获取客户的电子邮件地址:

$order = new WC_Order($order_id);
echo $order->get_billing_email();
Run Code Online (Sandbox Code Playgroud)

只是一个想法...

  • 最短和最佳答案。我使用`echo $ order-> get_billing_phone();`获得`billing_phone`。 (2认同)

Htm*_*sin 9

虽然,这可能不可取

如果您想获得客户详细信息.即使用户没有创建帐户但只生成订单.您可以直接从数据库中查询它.

虽然,可能存在性能问题,直接查询,但这肯定有效100%

你可以搜索post_id和meta_keys

 global $wpdb; // Get the global $wpdb
 $order_id = {Your Order Id}

 $table = $wpdb->prefix . 'postmeta';
 $sql = 'SELECT * FROM `'. $table . '` WHERE post_id = '. $order_id; 

        $result = $wpdb->get_results($sql);
        foreach($result as $res) {
            if( $res->meta_key == 'billing_phone'){
                   $phone = $res->meta_value;      // get billing phone
            }
            if( $res->meta_key == 'billing_first_name'){
                   $firstname = $res->meta_value;   // get billing first name
            }

            // You can get other values
            // billing_last_name
            // billing_email
            // billing_country
            // billing_address_1
            // billing_address_2
            // billing_postcode
            // billing_state

            // customer_ip_address
            // customer_user_agent

            // order_currency
            // order_key
            // order_total
            // order_shipping_tax
            // order_tax

            // payment_method_title
            // payment_method

            // shipping_first_name
            // shipping_last_name
            // shipping_postcode
            // shipping_state
            // shipping_city
            // shipping_address_1
            // shipping_address_2
            // shipping_company
            // shipping_country
        }
Run Code Online (Sandbox Code Playgroud)


小智 6

$customer_id = get_current_user_id();
print get_user_meta( $customer_id, 'billing_first_name', true );
Run Code Online (Sandbox Code Playgroud)

  • 请在您的答案中添加评论 (2认同)

Add*_*Ltd 6

WooCommerce"订单"只是一种自定义的帖子类型,因此所有订单都存储在wp_posts中,其订单信息存储在wp_postmeta表中.

如果您想获得WooCommerce"Order"的任何细节,那么您可以使用下面的代码.

$order_meta = get_post_meta($order_id); 
Run Code Online (Sandbox Code Playgroud)

上面的代码返回一个WooCommerce"Order"信息数组.您可以使用以下信息:

$shipping_first_name = $order_meta['_shipping_first_name'][0];
Run Code Online (Sandbox Code Playgroud)

查看"$ order_meta"数组中存在的所有数据.您可以使用以下代码:

print("<pre>");
print_r($order_meta);
print("</pre>");
Run Code Online (Sandbox Code Playgroud)


小智 5

我正在寻找这样的东西。效果很好。

因此,在 WooCommerce 插件中获取手机号码,如下所示 -

$customer_id = get_current_user_id();
print get_user_meta($customer_id, 'billing_phone', true);
Run Code Online (Sandbox Code Playgroud)