Jak uzyskać dane Klienta z zamówienia w WooCommerce?

Mam funkcję, która to robi:

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

Jak mogę uzyskać dane Klienta z tego?. Próbowałem wszystkiego w dokumentach, ale jakoś, tylko niektóre szczegóły są obecne, ale reszta nie jest, na przykład

$data['Address'] = $customer->get_address() . ' ' . $customer->get_address_2();
$data['ZipCode'] = $customer->get_postcode();

Jest pusty.

Robienie

var_dump($customer)

Produkuje:

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) "" ["adres_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) "" jest to bardzo ważne, ponieważ nie jest to możliwe. ["_changed": "WC_Customer": private] = > bool (false)}

Jak widzisz, miasto jest obecne, ale reszta jest pusta. Sprawdziłem w WP_usermeta i w panelu administracyjnym klienta i wszystkie dane są tam.

Jakieś pomysły?
Author: Alfonso Pérez, 2014-04-03

13 answers

Po wypróbowaniu $customer = new WC_Customer(); i global $woocommerce; $customer = $woocommerce->customer; nadal otrzymywałem puste dane adresowe, nawet gdy zalogowałem się jako użytkownik nie-admin.

Moje rozwiązanie było następujące:

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

...i ten kod działa niezależnie od tego, czy jesteś zalogowany jako admin, czy nie.

 18
Author: ban-geoengineering,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-14 09:37:03

Jeśli chcesz podać dane Klienta, które klient wprowadził podczas składania zamówienia, możesz użyć następującego kodu

$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

Poza Tym, $customer = new WC_Customer( $order_id ); nie można uzyskać dane Klienta

Po pierwsze, new WC_Customer() nie przyjmuje żadnych argumentów

Po Drugie, WC_Customer otrzyma dane Klienta tylko wtedy, gdy użytkownik jest zalogowany i nie jest po stronie Administratora, zamiast tego powinien znajdować się na stronie głównej witryny, takiej jak "Moje Konto", "Sklep", "Koszyk", "Kasa" strona

Mam nadzieję, że te informacje przyda się.

 27
Author: Ratnakar - StoreApps,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-03 16:46:22

chociaż może to nie być wskazane

Jeśli chcesz uzyskać dane Klienta. Nawet wtedy, gdy użytkownik nie tworzy konta, a jedynie składa zamówienie. Możesz po prostu odpytywać, bezpośrednio z bazy danych.

Chociaż mogą wystąpić problemy z wydajnością, pytając bezpośrednio, ale to na pewno działa w 100%

Możesz wyszukiwać według post_id i 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
        }
 9
Author: Html Tosin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-14 22:51:31

Może spójrz na Woocomerce Order klasy? Na przykład, aby uzyskać adres e-mail Klienta:

$order = new WC_Order($order_id);
echo $order->get_billing_email();
Tak sobie pomyślałem...
 7
Author: CodingCaio,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-29 17:44:41

WooCommerce "zamówienia" są tylko niestandardowym typem posta, więc wszystkie zamówienia są przechowywane w wp_posts, a jego informacje o zamówieniu w zapisanych w tabelach wp_postmeta.

Jeśli chcesz uzyskać szczegóły WooCommerce "Zamów", możesz użyć poniższego kodu.

$order_meta = get_post_meta($order_id); 

Powyższy kod zwraca tablicę informacji WooCommerce "zamówienie". Możesz użyć tych informacji, jak pokazano poniżej:

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

Aby wyświetlić wszystkie dane w tablicy "$ order_meta". Możesz użyć poniższego kodu:

print("<pre>");
print_r($order_meta);
print("</pre>");
 6
Author: AddWeb Solution Pvt Ltd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-23 11:17:51
$customer_id = get_current_user_id();
print get_user_meta( $customer_id, 'billing_first_name', true );
 5
Author: Рома Нечаев,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-17 12:52:50

Dzieje się tak, ponieważ Streszczenie WC_Customer nie przechowuje danych adresowych hold (między innymi danych) poza sesją. Dane te są przechowywane przez strony koszyka/kasy, ale ponownie-tylko w sesji (o ile chodzi o klasę WC_Customer).

Jeśli przyjrzysz się, jak strona kasy otrzymuje Dane klienta, przejdziesz do metody klasy WC_Checkoutget_value, który wyciąga go bezpośrednio z user meta . Dobrze by było, gdybyś podążał za tym samym wzór: -)

 4
Author: Josh Levinson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-12 20:36:58

Wyglądałem tak. Działa dobrze. Więc zdobądź numer telefonu w wtyczce woocommerce w ten sposób -

$customer_id = get_current_user_id();
print get_user_meta( $customer_id, 'billing_phone', true );
 4
Author: Vikas Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-31 16:25:41

Trochę za późno, ale właśnie się tym zająłem i natknąłem się na ten post. W zależności od tego, czego naprawdę chcesz, możesz uzyskać szczegóły z zamówienia w następujący sposób:

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

Gdzie $field_name to '_billing_address_1' lub '_shipping_address_1 ' _first_name'. Możesz wygooglować pozostałe pola, ale nie zapomnij " _ " na początku.

Jeśli chcesz odzyskać klienta dla tego zamówienia i uzyskać jego pole bezpośrednio, działa to tak, jak w Twoim rozwiązaniu, z wyjątkiem tego, że nie musisz pobierać pełny obiekt klienta:

$customer_id = (int)$order->user_id;

$field= get_user_meta($customer_id, $field_name,true)

; W tym przypadku $field_name nie zaczyna się od "_" Na przykład: 'first_name', and'billing_address_1'

 4
Author: Pieter van Kampen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-22 21:59:50

Tutaj w odpowiedź LoicTheAztec jest pokazany, jak odzyskać te informacje.

Tylko dla Woocommerce v3.0+

W zasadzie możesz zadzwonić

// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );

Spowoduje to zwrócenie tablicy do danych zamówienia rozliczeniowego, w tym właściwości rozliczeń i wysyłki. Zbadaj go przez var_dump-ing it. Oto przykład:

$order_billing_data = array(
    "first_name" => $order_data['billing']['first_name'],
    "last_name" => $order_data['billing']['last_name'],
    "company" => $order_data['billing']['company'],
    "address_1" => $order_data['billing']['address_1'],
    "address_2" => $order_data['billing']['address_2'],
    "city" => $order_data['billing']['city'],
    "state" => $order_data['billing']['state'],
    "postcode" => $order_data['billing']['postcode'],
    "country" => $order_data['billing']['country'],
    "email" => $order_data['billing']['email'],
    "phone" => $order_data['billing']['phone'],
);
Pozdrawiam.
 2
Author: manuman94,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-19 09:41:33

I kolejny przykład pobierania danych Klienta z bazy danych:

$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 );
 1
Author: Mostafa Soufi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-18 17:06:59

Pobierz identyfikator klienta z obiektu zamówienia

$order = new WC_Order($order_id);

// here the customer data
$customer = get_userdata($order->customer_user);
echo $customer->display_name;
 0
Author: Lafif Astahdziq,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-22 10:58:45

To stare pytanie i nie znalazłem tu dobrej odpowiedzi, ale udało mi się je rozgryźć.

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

Wiem na pewno, czy wysyłka e-mail jest częścią metadanych, ale jeśli tak, wolałbym go mieć niż e-mail rozliczeniowy-przynajmniej dla moich celów.

 0
Author: fwho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-15 13:00:17