如何在页面上查询订单

enxuqcxy  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(580)

我想创建一个页面,显示来自数据库的查询,以显示一些订单详细信息。
如何在页面上显示此查询?

i34xakig

i34xakig1#

获取订单有多种方式:
1) 商业有专门的功能 wc_get_orders() 这将为您提供一个wc\u order对象数组:

  1. $orders = wc_get_orders( array('numberposts' => -1) );
  2. // Loop through each WC_Order object
  3. foreach( $orders as $order ){
  4. echo $order->get_id() . '<br>'; // The order ID
  5. echo $order->get_status() . '<br>'; // The order status
  6. }

要获取订单数据,请参见下面的链接
2) 你也可以使用wordpress WP_Query :

  1. $loop = new WP_Query( array(
  2. 'post_type' => 'shop_order',
  3. 'post_status' => array_keys( wc_get_order_statuses() ),
  4. 'posts_per_page' => -1,
  5. ) );
  6. // The Wordpress post loop
  7. if ( $loop->have_posts() ):
  8. while ( $loop->have_posts() ) : $loop->the_post();
  9. // The order ID
  10. $order_id = $loop->post->ID;
  11. // Get an instance of the WC_Order Object
  12. $order = wc_get_order($loop->post->ID);
  13. endwhile;
  14. wp_reset_postdata();
  15. endif;

要获取订单数据,请参见下面的链接
3) 您可以使用sql查询

  1. global $wpdb;
  2. $results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE 'shop_order'");
  3. // Loop through each order post object
  4. foreach( $results as $result ){
  5. $order_id = $result->ID; // The Order ID
  6. // Get an instance of the WC_Order Object
  7. $order = wc_get_order( $result->ID );
  8. }

您将能够从 WC_Order 对象,如中所述:
如何获取订单详细信息
在woocommerce 3中获取订单项目和wc\订单项目\产品
将数据显示为表中的列表
要在列表中显示订单,您应该查看不推荐使用的模板myaccount/my-orders.php或myaccount/orders.php模板…
它会给你一个模型…

展开查看全部
u4vypkhs

u4vypkhs2#

只要是最后执行的查询,就可以使用 $wpdb->last_query 获取最后一个查询。你需要 define('SAVEQUERIES', true) 在你的 wp-config.php 为了这个

相关问题