【问题标题】:Customer Details顾客信息
【发布时间】:2015-01-23 00:54:30
【问题描述】:

与客户相关的woocommerce 问题。尽管 $order 对象中的数据对我不可用,但这里也有类似的帖子?

Get customers name in Woocommerce

问题:我需要在购买和管理订单状态更改时实时获取客户详细信息(任何客户 - 登录或一次性)。

例如未登录的客户购买产品。一旦产品状态“完成”,我就会捕捉到这个动作并在函数中做一些事情

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() 对象)

【问题讨论】:

  • stackoverflow.com/questions/17819054/… ...对不起,抽象类似乎有一些我看不到的变量....这可能是我应该继续的地方,尽管我的一生我在任何地方都看不到 billing_email 作为成员变量???

标签: wordpress woocommerce


【解决方案1】:

Woocommerce 类有神奇的 getter 和 setter。这意味着您无法使用 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 );

因此,如果您想查看订单的所有元数据(适用于产品/优惠券等),您必须直接询问数据库以查看哪些数据可用,然后您可以在您的代码。

希望对你有帮助

【讨论】:

  • 感谢@XciD 的正确定义和回答。
猜你喜欢
  • 1970-01-01
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
  • 2012-03-22
  • 2013-11-09
  • 2014-03-03
  • 2012-01-02
  • 1970-01-01
相关资源
最近更新 更多