我有点冒险进入这个未知领域。有没有办法查询所有WooCommerce订单(来自所有用户)?并将其显示在表格中,类似于管理员订单页面中的方式?我试着研究Admin'Orders'页面是如何工作的,但实际上非常不成功。 任何帮助都将非常感谢!
答案 0 :(得分:1)
我认为一个好的开始应该是使用以下args构建WP_query:
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status', 'field' => 'slug',
'terms' => array('processing')
)
)
);
答案 1 :(得分:1)
这是我如何开始的。但是,我现在对$result = mysqli_query($connection, $query);
echo " <table >";
$row = mysqli_fetch_assoc($result);
echo "<tr>";
foreach($row as $key => $val)
echo"<th>$key</th> ";
echo "</tr>";
$result = mysqli_query($connection, $query);
echo"<tr>";
while($row = mysqli_fetch_assoc($result))
{
foreach($row as $key => $val)
{
echo "<td>$val</td>";
}
echo "</tr>";
}
echo "</table>";
的理解还不够深入。
这需要在插件中:
WP_List_Table
然后在插件根目录中的另一个名为function orders_redux_menu(){
if ( current_user_can( 'manage_woocommerce' ) ) {
add_submenu_page( 'woocommerce', __( 'Orders Part Deux', 'your-plugin-textdomain' ), __( 'Orders Part Deux', 'your-plugin-textdomain' ) , 'manage_woocommerce', 'wc-orders-redux', 'orders_redux_page' );
}
}
add_action( 'admin_menu', 'orders_redux_menu', 15 );
function orders_redux_page() {
if ( ! class_exists( 'WP_Posts_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-posts-list-table.php' );
}
include_once('orders-table-redux.php');
$new_table = new Orders_Redux_List();
?>
<div class="wrap">
<h2><?php _e( "Orders Redux", "your-plugin-textdomain" );?></h2>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div id="post-body-content">
<div class="meta-box-sortables ui-sortable">
<form method="post">
<?php
$new_table->prepare_items();
$new_table->display(); ?>
</form>
</div>
</div>
</div>
<br class="clear">
</div>
</div>
<?php
}
orders-table-redux.php
<?php
class Orders_Redux_List extends WP_Posts_List_Table {
static $post_type = 'shop_order';
/** Class constructor */
public function __construct() {
//$screen = convert_to_screen( 'wc-orders-redux' );
parent::__construct( [
'singular' => __( 'Order Redux', 'sp' ), //singular name of the listed records
'plural' => __( 'Orders Redux', 'sp' ), //plural name of the listed records
'ajax' => false, //should this table support ajax?
'screen' => 'edit-' . self::$post_type // this doesn't do what I'd hoped yet
] );
}
}
有许多方法可以覆盖以自定义列,结果等。
以下是自定义表格实施的示例教程:https://www.sitepoint.com/using-wp_list_table-to-create-wordpress-admin-tables/
答案 2 :(得分:1)
我认为,您希望所有订单列表都包含产品详细信息和客户详细信息等,因此您必须创建自己的自定义查询以使结果显示在报告中。
如下面的查询,我也用于我的项目。
所以它会对你有所帮助。
SELECT
p.ID as order_id,
p.post_date,
max( CASE WHEN pm.meta_key = '_billing_email' AND p.ID = pm.post_id THEN pm.meta_value END ) as billing_email,
max( CASE WHEN pm.meta_key = '_billing_first_name' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_first_name,
max( CASE WHEN pm.meta_key = '_billing_last_name' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_last_name,
max( CASE WHEN pm.meta_key = '_billing_address_1' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_1,
max( CASE WHEN pm.meta_key = '_billing_address_2' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_2,
max( CASE WHEN pm.meta_key = '_billing_city' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_city,
max( CASE WHEN pm.meta_key = '_billing_state' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_state,
max( CASE WHEN pm.meta_key = '_billing_postcode' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_postcode,
max( CASE WHEN pm.meta_key = '_shipping_first_name' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_first_name,
max( CASE WHEN pm.meta_key = '_shipping_last_name' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_last_name,
max( CASE WHEN pm.meta_key = '_shipping_address_1' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_address_1,
max( CASE WHEN pm.meta_key = '_shipping_address_2' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_address_2,
max( CASE WHEN pm.meta_key = '_shipping_city' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_city,
max( CASE WHEN pm.meta_key = '_shipping_state' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_state,
max( CASE WHEN pm.meta_key = '_shipping_postcode' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_postcode,
max( CASE WHEN pm.meta_key = '_order_total' AND p.ID = pm.post_id THEN pm.meta_value END ) as order_total,
max( CASE WHEN pm.meta_key = '_order_tax' AND p.ID = pm.post_id THEN pm.meta_value END ) as order_tax,
max( CASE WHEN pm.meta_key = '_paid_date' AND p.ID = pm.post_id THEN pm.meta_value END ) as paid_date,
( SELECT GROUP_CONCAT( order_item_name separator '|' ) FROM wp_woocommerce_order_items WHERE order_id = p.ID ) as order_items
FROM
wp_posts p
JOIN wp_postmeta pm on p.ID = pm.post_id
JOIN wp_woocommerce_order_items oi on p.ID = oi.order_id
WHERE
post_type = 'shop_order' AND
post_status = 'wc-completed'
GROUP BY
p.ID