4

I want to get all order amounts and the payment date of orders made by current user inside a plugin.

I'm using this code, it currently prints order values: Like this 150001000 where 1st order is 0(starting from right) and 2nd is 100 and third is 15000. What I am trying to accomplish is putting orders amounts and dates in variables.

$customer = wp_get_current_user();
// Get all customer orders
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key'    => '_customer_user',
'orderby' => 'date',
'order' => 'DESC',
'meta_value'  => get_current_user_id(),
'post_type'   => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),  'post_status' 
=> array( 'wc-processing'),
) );


//echo count( $customer_orders );
foreach ( $customer_orders as $customer_order ) {
$orderq = wc_get_order( $customer_order );
$totalq = $orderq->get_total();
echo $totalq;       
} 
user2940097
  • 63
  • 1
  • 1
  • 6

3 Answers3

8

Below you can find the code which will give you an array with orders value date and id

 $customer = wp_get_current_user();
// Get all customer orders
    $customer_orders = get_posts(array(
        'numberposts' => -1,
        'meta_key' => '_customer_user',
        'orderby' => 'date',
        'order' => 'DESC',
        'meta_value' => get_current_user_id(),
        'post_type' => wc_get_order_types(),
        'post_status' => array_keys(wc_get_order_statuses()), 'post_status' => array('wc-processing'),
    ));

    $Order_Array = []; //
    foreach ($customer_orders as $customer_order) {
        $orderq = wc_get_order($customer_order);
        $Order_Array[] = [
            "ID" => $orderq->get_id(),
            "Value" => $orderq->get_total(),
            "Date" => $orderq->get_date_created()->date_i18n('Y-m-d'),
        ];

    }

Basically what we did here is just storing the values you want in array so you can use them later somewhere in your script

Output

Array
(
[0] => Array
    (
        [ID] => 136
        [Value] => 240.00
        [Date] => 2018-08-13
    )

[1] => Array
    (
        [ID] => 116
        [Value] => 97.99
        [Date] => 2018-08-10
    )

 )
kashalo
  • 3,442
  • 2
  • 11
  • 28
6

I solved my need with that approach (wc_get_orders is recommended by the vendor for using):

    public function get_sum_of_paid_orders( int $user_id ): int {

        $customer_orders = [];
        foreach ( wc_get_is_paid_statuses() as $paid_status ) {
            $customer_orders += wc_get_orders( [
                'type'        => 'shop_order',
                'limit'       => - 1,
                'customer_id' => $user_id,
                'status'      => $paid_status,
            ] );
        }

        $total = 0;
        foreach ( $customer_orders as $order ) {
            $total += $order->get_total();

            // your code is here
        }

        return $total;
    }
Paul Burilichev
  • 406
  • 3
  • 10
-4

You can get the order date by using the code below

$order = new WC_Order($order_id);
$order_date = $order->order_date;
Yogesh Garg
  • 299
  • 3
  • 10
  • I am not that proficient in woocommerce, can you suggest how do I edit the code above to retrieve date and amount as array. – user2940097 Aug 23 '18 at 07:24