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

前端 未结 15 1463
北海茫月
北海茫月 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:53

    If you want customer's details that customer had entered while ordering, then you can use the following code:

    $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 the web page
    $shipping_address = $order->get_shipping_address();
    $shipping_address_html = $order->get_formatted_shipping_address(); // For printing or displaying on web page
    

    Apart from this, $customer = new WC_Customer( $order_id ); can not get you customer details.

    First of all, new WC_Customer() doesn't take any arguments.

    Secondly, WC_Customer will get customer's details only when the user is logged in and he/she is not on the admin side. Instead he/she should be on website's front-end like the 'My Account', 'Shop', 'Cart', or 'Checkout' page.

    0 讨论(0)
  • 2020-12-01 00:53

    Maybe look at the WooCommerce Order class? For example, to get the customer's email address:

    $order = new WC_Order($order_id);
    echo $order->get_billing_email();
    

    Just a thought...

    0 讨论(0)
  • 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
            }
    
    0 讨论(0)
  • 2020-12-01 00:57

    WooCommerce "Orders" are just a custom post type, so all the orders are stored in wp_posts and its order information in stored into wp_postmeta tables.

    If you would like to get any details of WooCommerce's "Order" then you can use the below code.

    $order_meta = get_post_meta($order_id); 
    

    The above code returns an array of WooCommerce "Order" information. You can use that information as shown below:

    $shipping_first_name = $order_meta['_shipping_first_name'][0];
    

    To view all data that exist in "$order_meta" array, you can use the below code:

    print("<pre>");
    print_r($order_meta);
    print("</pre>");
    
    0 讨论(0)
  • 2020-12-01 00:59

    This is happening because the WC_Customer abstract doesn't hold hold address data (among other data) apart from within a session. This data is stored via the cart/checkout pages, but again—only in the session (as far as the WC_Customer class goes).

    If you take a look at how the checkout page gets the customer data, you'll follow it to the WC_Checkout class method get_value, which pulls it directly out of user meta. You'd do well to follow the same pattern :-)

    0 讨论(0)
  • 2020-12-01 01:00

    And another example to get the customer details from the database:

    $order = new WC_Order($order_id);
    $order_detail['status']              = $order->get_status();
    $order_detail['customer_first_name'] = get_post_meta($order_id, '_billing_first_name', true);
    $order_detail['customer_last_name']  = get_post_meta($order_id, '_billing_last_name', true);
    $order_detail['customer_email']      = get_post_meta($order_id, '_billing_email', true);
    $order_detail['customer_company']    = get_post_meta($order_id, '_billing_company', true);
    $order_detail['customer_address']    = get_post_meta($order_id, '_billing_address_1', true);
    $order_detail['customer_city']       = get_post_meta($order_id, '_billing_city', true);
    $order_detail['customer_state']      = get_post_meta($order_id, '_billing_state', true);
    $order_detail['customer_postcode']   = get_post_meta($order_id, '_billing_postcode', true);
    
    0 讨论(0)
提交回复
热议问题