1

Good Day, I'm trying to get the list of recent active orders by a user, order ID and get the status of each of the active order in woocommerce, then print out any update in status of any of the orders. I have searched around for clues or snippet that would help me achieve this, but couldn't find one.

I was able to get how to count the number of order (using the below snippet)

$current_user = wp_get_current_user();
$numorders = wc_get_customer_order_count( $current_user->ID );

After searching on various blogs, i was able to this;

$order = wc_get_order( $order_id );    
$order->get_status()

I'm really clueless how to combine this to print out the desired result (which is to have a list of notification update of any of the recent order(s) by a user.

The Goal of this is to create something like a mini order notification system where the result will look something like this;

Your ORDER NOTIFICATION

=> Your order with ID #24555 is being completed - 15-01-2019
=> Your order with ID #24234 is being completed - 13-01-2019
=> Your order with ID #24555 has been shipped - 10-01-2019
=> Your order with ID #24234 has been shipped - 10-01-2019
=> Your order with ID #12324 is being processed - 09-01-2019

[Maximum of 10 notification]

Thanks

jasie
  • 2,192
  • 10
  • 39
  • 54
  • I think you have to get all orders of a customer and then check the status of each order. Do you know how to to that? Check here: https://www.skyverge.com/blog/get-all-woocommerce-orders-for-a-customer/ – jasie Jan 14 '19 at 09:16
  • WooCommerce keep the order status in `wp_posts.post_status` table, and when ever you update the status or an order this fields get updated, so directly you cannot built this system, but there are 2 solutions number *one* which is inbuilt you can check the `wp_comments` table where all the order notes and status are logged as a comment, so you can filter out according to your need. *second* you can build your own table and logic where you will insert an order change as list it according to your needs. – Raunak Gupta Jan 14 '19 at 09:29
  • Thanks @jasie for you response, I have gone through the link. The example on the page prints the message as a notice. How would I print the result so that I can have maximum of 10 notifications at ones and when order notification is more than 10, older notification should go away/be removed/deleted – Kolawole Emmanuel Izzy Jan 14 '19 at 09:40
  • @raunak-gupta Please can I have a snippet guide that I can work with? My pho level isn't too great yet – Kolawole Emmanuel Izzy Jan 14 '19 at 09:42
  • Set 'numberposts' => 10 returns maximum of 10 orders. – jasie Jan 14 '19 at 09:42
  • Thanks @jasie, trying it out now – Kolawole Emmanuel Izzy Jan 14 '19 at 09:46

1 Answers1

0
  1. Get all orders of a customer, like so:

    $customer_orders = get_posts( array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => get_current_user_id(), 'post_type' => wc_get_order_types(), 'post_status' => array_keys( wc_get_order_statuses() ), ) );

  2. Go through all orders and check the status of each order, like so:

    $order->get_status( )

  3. Print out the ID and status and date as desired

jasie
  • 2,192
  • 10
  • 39
  • 54