Leo C
Leo C

Reputation: 93

Printing all WooCommerce Orders (for admin users)

I'm kind of venturing into uncharted territories with this one. Is there any way to get a query of all WooCommerce orders (from all users)? And have it displayed in a table, similar to how it is in the Admin 'Orders' page? I tried to study how the Admin 'Orders' page works but that was actually very unsuccessful. Any help at all would be greatly appreciated!

Upvotes: 0

Views: 2005

Answers (3)

vjy tiwari
vjy tiwari

Reputation: 861

I think , you want all orders list with product details , and customer details etc , So you have to create own custom Query to get the result to display in the report .

As below query i also used for my project.

So it will be helpful to you .

        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

Upvotes: 2

helgatheviking
helgatheviking

Reputation: 26329

Here's how I would start. But, I don't have a deep enough understanding of WP_List_Table right now to fill it out.

This needs to be in a plugin:

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

}

And then in another file in the plugin root called 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 has a lot of methods you can override to customize the columns, the results, etc.

Here's an example tutorial of a custom table implementation: https://www.sitepoint.com/using-wp_list_table-to-create-wordpress-admin-tables/

Upvotes: 1

jjj
jjj

Reputation: 1005

I think that a good start should be to build a WP_query with the following args:

$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')                           
        )                     
    )           
);

Upvotes: 1

Related Questions