首页 > 解决方案 > woocommerce:如何访问数据库中的订单购买

问题描述

我正在尝试从我的 wordpress 管理插件访问用户购买信息,以便我可以总结订单,例如:

它主要是用户登录时在“帐户”>“订单”页面上可以找到的信息。

我查看了 woocommerce 表,但找不到此信息。

你能建议我可以查询哪些表来汇总我在上面寻找的信息吗?

标签: wordpresswoocommerce

解决方案


订单以“shop_order”自定义帖子类型存储在wp_posts表中。您可以像这样简单地使用 woocommerce 功能。

$woo_orders = wc_get_orders( array('numberposts' => -1) );

/* Loop each WC_Order object */
foreach( $woo_orders $order ){

  /* Get the ID */
  echo $order->get_id();

  /* Get the status */
  echo $order->get_status(); // The status
}

或者使用普通的 wordpress 循环:

$loop = new WP_Query( array(
   'post_type'         => 'shop_order',
   'posts_per_page'    => -1,
   'post_status'       =>  'wc-ywraq-new' //will get the new order
) );

// Your post loop
if ( $loop->have_posts() ): 
  while ( $loop->have_posts() ) : $loop->the_post();

    // The ID
    $order_id = $loop->post->ID;

    // The object from WC_Order find the reference in woocommerce docs
    $order = wc_get_order($loop->post->ID);

  endwhile;

  wp_reset_postdata(); // always

endif;

这是来自 github 的参考:https ://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query


推荐阅读