如何在页面上查询 Woocommerce 订单 [英] How to query Woocommerce orders on a page
问题描述
我想创建一个页面来显示数据库中的查询以显示一些订单详细信息.
I want to create a page that displays a query from the database to show some order details.
如何在页面上显示此查询?
How to display this query on a page?
推荐答案
Woocommerce Orders 有多种获取方式:
There is multiple ways to get Woocommerce Orders:
1) Woocommerce 有一个专门的函数 wc_get_orders()
这会给你一个 WC_Order 对象的数组:
1) Woocommerce has a dedicated function wc_get_orders()
that will give you an array of WC_Order objects:
$orders = wc_get_orders( array('numberposts' => -1) );
// Loop through each WC_Order object
foreach( $orders as $order ){
echo $order->get_id() . '<br>'; // The order ID
echo $order->get_status() . '<br>'; // The order status
}
要获取订单数据,请参阅下面的链接
To get the order data see the links below
2) 您也可以使用 Wordpress WP_Query
:
2) You can also use a Wordpress WP_Query
:
$loop = new WP_Query( array(
'post_type' => 'shop_order',
'post_status' => array_keys( wc_get_order_statuses() ),
'posts_per_page' => -1,
) );
// The Wordpress post loop
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post();
// The order ID
$order_id = $loop->post->ID;
// Get an instance of the WC_Order Object
$order = wc_get_order($loop->post->ID);
endwhile;
wp_reset_postdata();
endif;
要获取订单数据,请参阅下面的链接
To get the order data see the links below
3) 您可以使用 SQL 查询
3) You can use a SQL query
global $wpdb;
$results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE 'shop_order'");
// Loop through each order post object
foreach( $results as $result ){
$order_id = $result->ID; // The Order ID
// Get an instance of the WC_Order Object
$order = wc_get_order( $result->ID );
}
<小时>
您将能够从 WC_Order<获取所有订单详细信息/code> object
解释如下:
将数据显示为表格中的列表
要在列表中显示订单,您应该查看已弃用的 woocommerce 模板 myaccount/my-orders.php 或 myaccount/orders.php 模板...
To display the orders in a list, you should have a look to the woocommerce deprecated template myaccount/my-orders.php or to myaccount/orders.php template…
它会给你一个模型......
It will give you a model…
这篇关于如何在页面上查询 Woocommerce 订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!