2017-07-27 69 views
0

我有点冒险进入这个未知领域。有没有办法获得所有WooCommerce订单(来自所有用户)的查询?并将它显示在表格中,类似于Admin'订单'页面中的内容?我试图研究Admin'Orders'页面是如何工作的,但实际上很不成功。 任何帮助都将不胜感激!打印所有WooCommerce订单(对于管理员用户)

+0

为什么?你好像只是复制现有的页面? – helgatheviking

+0

@helgatheviking我不会以同样的方式使用它。我正在开发的功能店的方式非常复杂,不同的用户有不同的产品库存。因此,我需要至少获得所有订单的查询,以便为管理员创建另一个完整的自定义库存报告。 –

回答

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

这是我会怎么开始。但是,我现在还没有深入了解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 
     ]); 

    } 

} 

WP_List_Table有很多,你可以重写自定义列的方法,结果等

以下是定制表实现的示例教程:https://www.sitepoint.com/using-wp_list_table-to-create-wordpress-admin-tables/

+0

谢谢你的回答。我一直在尝试使用此代码大约一个小时。虽然我还没有取得任何成功,但我会继续研究这一点。这绝对是一个好的起点! –

+0

这是一个模糊的起点,我知道。为此我很抱歉。给自己一个休息,然后用新鲜的眼睛回来。 – helgatheviking

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 
+0

这绝对有趣。谢谢您的回答。现在裸露在我身边,这将用于何处?我的意思是我明白一切都在做什么,但我不知道如何去实现这个... –

+0

创建您自己的插件来显示订单报告,并根据需要使用WP_List_Table类来显示报告数据。 –

相关问题