How can I get customer details from an order in WooCommerce?

前端 未结 15 1549
北海茫月
北海茫月 2020-12-01 00:10

I have a function that does this:

$order = new WC_Order($order_id);
$customer = new WC_Customer($order_id);

How can I get customer details fr

15条回答
  •  醉话见心
    2020-12-01 00:54

    Although, this may not be advisable.

    If you want to get customer details, even when the user doesn’t create an account, but only makes an order, you could just query it, directly from the database.

    Although, there may be performance issues, querying directly. But this surely works 100%.

    You can search by post_id and 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
            }
    

提交回复
热议问题