wordpress | wordpress – Zachary Powell
Zachary Powell

Software Programmer

Zachary Powell

Download my CV Send me a Message


Access Woo-commerce order Information

Zachary Powell - 01/02/2016


Recently, I have started using Woo-commerce more and more. In fact it has now become my go to standard for custom e-commerce websites, from simple sites that offer only a few products to large sites with 100’s of items. I have found this plug-in to be flexible enough to accommodate any requirements a client might have, be it custom payment gateways or even extra processing logic.

Being based on WordPress, it is as easy to override any of the woo-commerce code or use any of the many hooks to expand the code base.

I will do a number of blog posts around this plugin, exploring many of the different ways you are able to expand and change the code. Today we will be looking at how we can access a logged in users past orders and display that information on a custom page.

We are going to create a page called “Get Orders”, give it the slug “get-orders”, then in your sites theme directory create a file called page-get-orders.php.

File Setup

Start off with the top comment section. As with all WordPress theme files, a very basic example would be:

 * Template Name: Get Orders
 * @package myCustom-theme

Here we just give the file a name and assign it to the theme page.

$order_id = get_query_var( 'id' );

Next, we grab the ID of the order we actually want to look at. This is passed to the page as a get variable, so an example URL would be: woocommerce-website.co.uk/get-orders/?id=142

We might just want to see all the orders, but this is optional.

Get orders for logged in user

$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
  'numberposts' => $order_count,
  'meta_key'    => '_customer_user',
  'meta_value'  => get_current_user_id(),
  'post_type'   => wc_get_order_types( 'view-orders' ),
  'post_status' => array_keys( wc_get_order_statuses() )
) ) );

Now that the page is set up, we will run the actual code to get the orders. Using the WordPress get_post function and applying the ‘woocommerce_my_account_my_orders_query’, we are able to query Woocommerce for orders. Passing it the current users ID against _customer_user, we are given an array of all the current logged in users orders. If no user is logged in, this will not return any orders.

Access the Information

foreach ( $customer_orders as $customer_order ) {
    $order = wc_get_order( $customer_order );
    $order->populate( $customer_order );
    $item_count = $order->get_item_count();
    if ($order->get_order_number() == $order_id){
      $items = $order->get_items();
                        echo $order->billing_first_name;
                        echo $order->billing_last_name; 

We now have an array of orders $customer_orders, we can cycle through them. Taking each one, we get the order and then can check if it is the order ID we are looking for. Again, we might skip this step if we just want to print out the information of all orders.

Now that we have a WC_Order object ($order), we have access to a huge range of very useful fields and methods. Check out https://docs.woothemes.com/wc-apidocs/class-WC_Order.html for a look at these. The above example code has just a few but there are many many more.

Leave a Reply

Your email address will not be published. Required fields are marked *

Tags: ,

“ Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ”

-Martin Fowler