WooCommerce 小部件 - 从日期到日期的订单列表
WooCommerce widget - List of orders from date to date
我想创建一个仪表板小部件来显示当月上半月的订单,这是我的代码:
add_action( 'wp_dashboard_setup', 'register_my_dashboard_widget' );
function register_my_dashboard_widget() {
wp_add_dashboard_widget(
'my_dashboard_widget',
'Ordiers',
'my_dashboard_widget_display'
);
}
function my_dashboard_widget_display() {
global $wpdb;
$date_from = '2016-09-01';
$date_to = '2016-09-15';
$post_status = implode("','", array('wc-processing', 'wc-completed') );
$result = $wpdb->get_results( "SELECT * FROM $wpdb->posts
WHERE post_type = 'shop_order'
AND post_date BETWEEN '{$date_from} 00:00:00' AND '{$date_to} 23:59:59'
");
echo "<pre>";
print_r($result);
}
如何给列表样式?
要提供列表样式,您需要在循环中迭代每个订单。您将在循环外回显 <ul>
HTML 标签,在循环内回显 <li>
HTML 标签循环。我已经在您的查询中添加了所需的订单状态。
我稍微修改了你的代码:
function my_dashboard_widget_display() {
global $wpdb;
$date_from = '2016-09-01';
$date_to = '2016-09-15';
$result = $wpdb->get_results( "
SELECT * FROM $wpdb->posts
WHERE post_type = 'shop_order'
AND post_status = 'wc-completed' OR post_status = 'wc-processing'
AND post_date BETWEEN '{$date_from} 00:00:00' AND '{$date_to} 23:59:59'
" );
echo '<ul>';
foreach($result as $value) {
// Getting WC order object
$order = wc_get_order( $value->ID );
// For example displaying order number and status
echo '<li>Order #'.$order->id. ' with status ' . $order->get_status() . '</li>';
}
echo '</ul>';
}
代码在您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
此代码已经过测试并且有效
我想创建一个仪表板小部件来显示当月上半月的订单,这是我的代码:
add_action( 'wp_dashboard_setup', 'register_my_dashboard_widget' );
function register_my_dashboard_widget() {
wp_add_dashboard_widget(
'my_dashboard_widget',
'Ordiers',
'my_dashboard_widget_display'
);
}
function my_dashboard_widget_display() {
global $wpdb;
$date_from = '2016-09-01';
$date_to = '2016-09-15';
$post_status = implode("','", array('wc-processing', 'wc-completed') );
$result = $wpdb->get_results( "SELECT * FROM $wpdb->posts
WHERE post_type = 'shop_order'
AND post_date BETWEEN '{$date_from} 00:00:00' AND '{$date_to} 23:59:59'
");
echo "<pre>";
print_r($result);
}
如何给列表样式?
要提供列表样式,您需要在循环中迭代每个订单。您将在循环外回显 <ul>
HTML 标签,在循环内回显 <li>
HTML 标签循环。我已经在您的查询中添加了所需的订单状态。
我稍微修改了你的代码:
function my_dashboard_widget_display() {
global $wpdb;
$date_from = '2016-09-01';
$date_to = '2016-09-15';
$result = $wpdb->get_results( "
SELECT * FROM $wpdb->posts
WHERE post_type = 'shop_order'
AND post_status = 'wc-completed' OR post_status = 'wc-processing'
AND post_date BETWEEN '{$date_from} 00:00:00' AND '{$date_to} 23:59:59'
" );
echo '<ul>';
foreach($result as $value) {
// Getting WC order object
$order = wc_get_order( $value->ID );
// For example displaying order number and status
echo '<li>Order #'.$order->id. ' with status ' . $order->get_status() . '</li>';
}
echo '</ul>';
}
代码在您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
此代码已经过测试并且有效