1

So I'm new to coding in Wordpress an woocommerce too!

I want to list all completed orders in the front end of a Wordpress site, order number and item ordered.

I have created a template file and I can get it run basic PHP such as echo and foreach, but I can't get my head around how to list completed orders!

however, the below code doesn't return any orders.

I found the commented out SQL statement which is more what I'm after i.e. return all orders that have a date of x in a text meta field. but I have no idea of how to include it in the template file!

<?php
/**
* Template Name: PPR List
*/
wp_head();
 
 Echo "testing";

//"SELECT post_id FROM wp_postmeta WHERE meta_key = 'arrival_date' AND meta_value <= $today"
$all_orders = get_posts( array(
    'numberposts' => $order_count,
    'post_type'   => wc_get_order_types( 'complete' ),
    'post_status' => array_keys( wc_get_order_statuses() )
) );

echo '<pre>'; print_r($array); echo '</pre>';

wp_footer();
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
Adrian
  • 1,089
  • 24
  • 51

1 Answers1

2

Using WC_Order_Query is a lighter and simpler way

wc_get_orders and WC_Order_Query provide a standard way of retrieving orders that is safe to use and will not break due to database changes in future WooCommerce versions.

Source: wc_get_orders and WC_Order_Query


So you get

$args = array(
    'status'       => 'completed', // Accepts a string: one of 'pending', 'processing', 'on-hold', 'completed', 'refunded, 'failed', 'cancelled', or a custom order status.
    'meta_key'     => 'arrival_date', // Postmeta key field
    'meta_value'   => $today, // Postmeta value field
    'meta_compare' => '<=', // Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘EXISTS’ (only in WP >= 3.5), and ‘NOT EXISTS’ (also only in WP >= 3.5). Values ‘REGEXP’, ‘NOT REGEXP’ and ‘RLIKE’ were added in WordPress 3.7. Default value is ‘=’.
);

$orders = wc_get_orders( $args );

foreach ( $orders as $order ) {
    $order_id = $order->get_id();
    echo '<p>' . $order_id . '</p>';
}

Might come in handy: How to get WooCommerce order details

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50