Query the woocommerce orders

Hi,

I was building a page where all orders in my store would be queried and display IDs, price, payment status etc.

I want to select the post type but can’t see the Order post type. The order post type is accessible to some plugins like ACF.

@bricksultimate
Can your Query Builder works for this?

1 Like

Hi Abey,
I have moved your thread to “how to” because it is not a bug.

You can use the query editor to query the orders:

return [
  'post_type'         => 'shop_order',
  'post_status'       => array_keys(wc_get_order_statuses()),
  'posts_per_page'    => -1,
];

1 Like

Use {post_title} - Unsure why my previous post was removed.

Hi,
I’m trying to achieve the same thing, but the loop stays empty. Any idea why ?

Hi,

Can you share screenshot?

I think Bricks Ultimate has an element to display query & display woocommece order

Here it is:

'post_type'         => 'shop_order',

Post_type “shop_order” has been declared ‘legacy’. HPOS storage for woocommerce seems to have a different database structure, and predefined queries on Briks like {woo_order_number} or {woo_order_date} seem also not work.

Installed Taxonomies and Post Types Documentation - WooCommerce HPOS storage on woocommerce.

I keep investigating on https://developer.woocommerce.com/docs/hpos-order-querying-apis/ to query orders on Bricks query loop.

1 Like

Thanks @paquito

I managed to make it work by activating the compatibility mode in the Woocommerce settings.
I don’t know if that’s a good practice though.

Any idea on how to call more dynamic data from the orders ?
{post_title} and {post_id} work fine.
But not {woo_order_total} for example

@ VictorMuller Do you mean HPOS compatibility mode?

Compatibility HPOS mode will duplicate the orders data from HPOS tables ( _wc_orders _wc_orders_meta…) to WP tables _post and _post_meta.

If there are a lot of orders (180000 on the page I’m working on currently) it may slow down queries to _post and post_meta aftecting page speed rank and SEO due to the redundant data. even if you are not querying orders. And duplicate the database size if there are a lot of orders.

I wrote an email to help@bricksbuilder.io asking for proper way to query HPOS order data.

1 Like

@paquito yes this is what I meant

I guess this is bad practice indeed. Even though I’m not expecting that many orders: Around 5000 per year.

Thanks for your time, looking forward to hearing from the Bricks team.

I got a response from Support:

The post query loop in Bricks is using WP_Query

You can test by using a new Code element and manually execute WP_Query + your own arguments.

If it doesn’t work, means the Woo HPOS is not supported via WP_Query.

There is no integration code in Bricks for Woo HPOS.

So I suggested a feature on Ideas Board to support HPOS. Hope it gets published, voted and implemented in the near future.

Meanwhile, one solution is to use the wc_get_orders function in a code element, which wouldn’t take advantage of the Bricks query loop.

This code can serve as a starting point:

$args = [
  'customer_id' => get_current_user_id(),
  'limit'       => 10,
  'orderby'     => 'date',
  'order'       => 'DESC',
];

$orders = wc_get_orders( $args );

foreach ( $orders as $order ) {
    echo 'Order Number: ' . $order->get_id() . '<br>';
    echo 'Date: ' . $order->get_date_created()->date('Y-m-d H:i:s') . '<br>';    
    echo 'Number of Items: ' . $order->get_item_count() . '<br>'; 
    echo 'Total: ' . $order->get_total() . '<br>';
    echo '<br>'; 
}
1 Like

Thanks again @paquito

Your code already helps a lot.

I couldn’t get solution since I opened the thread. I even forgot about it. Still no straightforward solution yet.

I saw the Bricks Ultimate (@bricksultimate ) plugin claiming to have the solution, but I haven’t tried it out.