我想创建一个仪表板小部件来显示当月前半部分的订单,这是我的代码:
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);
}
如何提供列表样式?
答案 0 :(得分:1)
要提供列表样式,您需要在循环中迭代每个订单。您将在循环外部回显 <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文件或任何插件文件中。
此代码经过测试和工作