与客户相关的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访问; etc ...(假设$ order是具有有效订单ID的WC_Order()对象)
答案 0 :(得分:2)
Woocommerce课程有魔术吸气剂和制定者。这意味着您无法通过var_dump查看数据,因为它可以根据需要从数据库动态获取。
订单对象有一个魔术函数: https://github.com/woothemes/woocommerce/blob/master/includes/abstracts/abstract-wc-order.php#L805
正如您所看到的,当您询问未知的元数据时,它会直接进入数据库。
离。如果你写:$order->billing_last_name
,你将产生:get_post_meta( $this->id, '_billing_last_name', true );
因此,如果您想查看订单的所有元数据(适用于产品/优惠券/等),您必须直接询问数据库以查看哪些数据可用,然后您可以正确使用它代码。
希望它有所帮助