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

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

    I just dealt with this. Depending on what you really want, you can get the details from the order like this:

    $field = get_post_meta($order->id, $field_name, true);
    

    Where $field_name is '_billing_address_1' or '_shipping_address_1'or 'first_name'. You can google the other fields, but don't forget the "" at the beginning.

    If you want to retrieve the customer for this order, and get its field directly, it works as in your solution, except you do not need to retrieve the full customer object:

    $customer_id = (int)$order->user_id;
    
    $field = get_user_meta($customer_id, $field_name, true);
    

    Now in this case, the $field_name does not start with "_". For example: 'first_name' and 'billing_address_1'.

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

    Get the customer id from the order object:

    $order = new WC_Order($order_id);
    
    // Here the customer data
    $customer = get_userdata($order->customer_user);
    echo $customer->display_name;
    
    0 讨论(0)
  • 2020-12-01 00:48

    I did manage to figure it out:

    $order_meta    = get_post_meta($order_id);
    $email         = $order_meta["_shipping_email"][0] ?: $order_meta["_billing_email"][0];
    

    I do know know for sure if the shipping email is part of the metadata, but if so I would rather have it than the billing email - at least for my purposes.

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

    WooCommerce is using this function to show billing and shipping addresses in the customer profile. So this will might help.

    The user needs to be logged in to get address using this function.

    wc_get_account_formatted_address( 'billing' );
    

    or

    wc_get_account_formatted_address( 'shipping' );
    
    0 讨论(0)
  • 2020-12-01 00:51

    I was looking for something like this. It works well.

    So get the mobile number in WooCommerce plugin like this -

    $customer_id = get_current_user_id();
    print get_user_meta($customer_id, 'billing_phone', true);
    
    0 讨论(0)
  • 2020-12-01 00:53

    Having tried $customer = new WC_Customer(); and global $woocommerce; $customer = $woocommerce->customer; I was still getting empty address data even when I logged in as a non-admin user.

    My solution was as follows:

    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;
    }
    

    ...and this code works regardless of whether you are logged in as admin or not.

    0 讨论(0)
提交回复
热议问题