2014-11-25 156 views
-1

與客戶有關的woocommerce問題。儘管$ order對象中的數據不適用於我,這裏也有類似的帖子嗎?客戶詳細信息

Get customers name in Woocommerce

問題:我需要得到客戶的詳細信息(任何客戶 - 登錄或一次性)在採購和管理訂單狀態的變化實時。

E.g.未登錄客戶購買產品。一旦產品狀態爲「已完成」我抓住這個動作,做功能

add_action('woocommerce_order_status_completed', 'woocommerce_payment_completed'); 

內的東西在這一點上我可以檢索訂單ID和相關的訂單最多的事。客戶的詳細信息雖然逃避我...

如果客戶登錄我可以使用$ user_id,但如果他們沒有在該網站上的帳戶,那麼我可以在哪裏可以得到他們的名字/電子郵件等?

嘗試一些東西......這裏是

$order->get_user(), new WC_Customer(), $order itself. 

結果輸出:

boolean false 
object(WC_Customer)[106] 
    protected '_data' => 
    array (size=14) 
     'country' => string 'AU' (length=2) 
     'state' => string '' (length=0) 
     'postcode' => string '' (length=0) 
     'city' => string '' (length=0) 
     'address' => string '' (length=0) 
     'address_2' => string '' (length=0) 
     'shipping_country' => string 'AU' (length=2) 
     'shipping_state' => string '' (length=0) 
     'shipping_postcode' => string '' (length=0) 
     'shipping_city' => string '' (length=0) 
     'shipping_address' => string '' (length=0) 
     'shipping_address_2' => string '' (length=0) 
     'is_vat_exempt' => boolean false 
     'calculated_shipping' => boolean false 
    private '_changed' => boolean false 
object(WC_Order)[134] 
    public 'id' => int 44 
    public 'order_type' => string 'simple' (length=6) 
    public 'prices_include_tax' => boolean true 
    public 'tax_display_cart' => string 'incl' (length=4) 
    public 'display_totals_ex_tax' => boolean false 
    public 'display_cart_ex_tax' => boolean false 
    public 'post' => 
    object(WP_Post)[132] 
     public 'ID' => int 44 
     public 'post_author' => string '1' (length=1) 
     public 'post_date' => string '2014-11-25 05:07:55' (length=19) 
     public 'post_date_gmt' => string '2014-11-25 05:07:55' (length=19) 
     public 'post_content' => string '' (length=0) 
     public 'post_title' => string 'Order – November 25, 2014 @ 05:07 AM' (length=42) 
     public 'post_excerpt' => string '' (length=0) 
     public 'post_status' => string 'wc-on-hold' (length=10) 
     public 'comment_status' => string 'open' (length=4) 
     public 'ping_status' => string 'closed' (length=6) 
     public 'post_password' => string 'order_54740eab99424' (length=19) 
     public 'post_name' => string 'order-nov-25-2014-0507-am' (length=25) 
     public 'to_ping' => string '' (length=0) 
     public 'pinged' => string '' (length=0) 
     public 'post_modified' => string '2014-11-25 05:07:55' (length=19) 
     public 'post_modified_gmt' => string '2014-11-25 05:07:55' (length=19) 
     public 'post_content_filtered' => string '' (length=0) 
     public 'post_parent' => int 0 
     public 'guid' => string 'http://essential.localtest.me/?post_type=shop_order&p=44' (length=61) 
     public 'menu_order' => int 0 
     public 'post_type' => string 'shop_order' (length=10) 
     public 'post_mime_type' => string '' (length=0) 
     public 'comment_count' => string '2' (length=1) 
     public 'filter' => string 'raw' (length=3) 
    public 'order_date' => string '2014-11-25 05:07:55' (length=19) 
    public 'modified_date' => string '2014-11-25 05:07:55' (length=19) 
    public 'customer_message' => string '' (length=0) 
    public 'customer_note' => string '' (length=0) 
    public 'post_status' => string 'wc-on-hold' (length=10) 
    public 'shipping_address' => string 'anothe address, Sydney, NSW, 2011, AU' (length=37) 
    public 'billing_address' => string 'anothe address, Sydney, NSW, 2011, AU' (length=37) 

編輯:!!!!所以,看來我對Wordpress的瞭解還是初學者。 似乎正在發生的事情是數據存儲在wp_postmeta表中。這些數據是可以訪問的(不知何故),我要找的字段(或meta_key)是_billing_first_name,_billing_last_name,可以通過$ order-> billing_last_name;等等...(假設$ order是具有有效訂單ID的WC_Order()對象)

+0

http://stackoverflow.com/questions/17819054/get-customers-name-in-woocommerce ...很抱歉,似乎抽象類可能有一些變量,我不能」 t看....這可能是我應該繼續的地方,儘管對於我來說,我無法將billing_email作爲成員變量的任何地方看到? – 2014-11-25 05:53:28

回答

2

Woocommerce類具有魔術獲取器和設置器。這意味着您無法使用var_dump查看數據,因爲它是從數據庫按需動態獲取的。

有個例神奇功能的訂貨對象: https://github.com/woothemes/woocommerce/blob/master/includes/abstracts/abstract-wc-order.php#L805

正如你所看到的,當你問一個未知的元數據,它直接進入數據庫。

ex。如果你寫:$order->billing_last_name你會產生:get_post_meta($this->id, '_billing_last_name', true);

所以,如果你想看到一個訂單的所有元數據(可在產品/優惠券/等),你必須直接查詢數據庫,以瞭解哪些數據可用,然後您可以在代碼上正確使用它。

希望這有助於

+0

感謝您的正確定義並回答@XciD。 – 2016-06-17 12:20:13