7

I want to create a page that displays a query from the database to show some order details.

How to display this query on a page?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Michelle
  • 549
  • 1
  • 5
  • 25

2 Answers2

18

There is multiple ways to get Woocommerce Orders:

1) Woocommerce has a dedicated function wc_get_orders() that will give you an array of WC_Order objects:

$orders = wc_get_orders( array('numberposts' => -1) );

// Loop through each WC_Order object
foreach( $orders as $order ){
    echo $order->get_id() . '<br>'; // The order ID
    echo $order->get_status() . '<br>'; // The order status
}

To get the order data see the links below


2) You can also use a Wordpress WP_Query:

$loop = new WP_Query( array(
    'post_type'         => 'shop_order',
    'post_status'       =>  array_keys( wc_get_order_statuses() ),
    'posts_per_page'    => -1,
) );

// The Wordpress post loop
if ( $loop->have_posts() ): 
while ( $loop->have_posts() ) : $loop->the_post();

// The order ID
$order_id = $loop->post->ID;

// Get an instance of the WC_Order Object
$order = wc_get_order($loop->post->ID);

endwhile;

wp_reset_postdata();

endif;

To get the order data see the links below


3) You can use a SQL query

global $wpdb;
$results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE post_type LIKE 'shop_order'");

// Loop through each order post object
foreach( $results as $result ){
    $order_id = $result->ID; // The Order ID

    // Get an instance of the WC_Order Object
    $order    = wc_get_order( $result->ID );
}

You will be able to get all order details from the WC_Order object as explained in:


Displaying the data as a list in a table

To display the orders in a list, you should have a look to the woocommerce deprecated template myaccount/my-orders.php or to myaccount/orders.php template

It will give you a model…

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • oh great... thank you! I like the 3rd option, as i have a custom query I want to use. I assume I would just replace your sql query "SELECT * FROM {$wpdb->prefix}p..." with the one I want to use? – Michelle Aug 21 '18 at 12:41
  • okay awesome.. and where do I actually put the whole piece of code you've provided? Does it go into functions.php? How do I actually make it display? – Michelle Aug 21 '18 at 12:47
  • @Michelle You can embed that in a custom shortcode function that you will put in your function.php… Then you will be able to use it everywhere to get a display. See [some examples of shortcodes functions](https://stackoverflow.com/search?q=user%3A3730754+shortcode+) – LoicTheAztec Aug 21 '18 at 13:17
  • cool.. thanks! Is there an easier way to add it? shortcodes are a little above my experience level! – Michelle Aug 21 '18 at 13:33
  • Done! I've also started a new question with my "Code" - https://stackoverflow.com/questions/51950316/woocommerce-shortcode-for-custom-query-not-working Thanks! – Michelle Aug 21 '18 at 13:55
  • @Michelle Answered your new question, tested and working… – LoicTheAztec Aug 21 '18 at 15:03
0

As long as it's the last query executed, you can use $wpdb->last_query to grab the last query. You'll need to have define('SAVEQUERIES', true) in your wp-config.php for this

Adam Mellen
  • 240
  • 2
  • 6
  • 1
    OP is not talking about the "last query" but how to run a query and display the results... – Lelio Faieta Aug 21 '18 at 11:18
  • Sorry... what I mean is.. i want to create a page the supplier can log into, that has all new orders listed, live. So i'm looking for the code I'd use to display the query on a page. Thanks! – Michelle Aug 21 '18 at 11:19