通过用户ID获取WooCommerce中单个客户的所有订单和订单数据

时间:2018-08-23 07:02:03

标签: php wordpress woocommerce

我想获取插件中当前用户的所有订单金额和订单付款日期。

我正在使用此代码,它当前会打印订单值: 像这样150001000 其中第一阶为0(从右开始),第二阶为100,第三阶为15000。 我要完成的工作是将订单金额和日期放入变量中。

$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;       
} 

3 个答案:

答案 0 :(得分:0)

您可以使用以下代码获取订单日期

$order = new WC_Order($order_id);
$order_date = $order->order_date;

答案 1 :(得分:0)

下面您将找到代码,该代码将为您提供带有订单值日期和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'),
        ];

    }

基本上我们在这里所做的只是将所需的值存储在数组中,以便以后可以在脚本中的某处使用它们

输出

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

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

 )

答案 2 :(得分:0)

我用这种方法解决了我的需要(供应商建议使用wc_get_orders

    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;
    }