File: /var/www/dk/wp-content/themes/food/api/order/r_user_order.php
<?php
$current_user = wp_get_current_user();
$current_id = $current_user->ID;
//echo $current_id;
$query = new WC_Order_Query(array(
'customer_id' => $current_id,
'orderby' => 'date',
'order' => 'DESC'
));
//print_r($query);
$order_list = [];
$orders = $query->get_orders();
//print_r($orders);
// Get 10 most recent order ids in date descending order.
//$args = array(
// 'customer_id' => 7,
//);
//$args = array(
//// 'customer' => 'zellop@qq.com',
// 'customer_id' => 6,
// 'orderby' => 'modified',
// 'order' => 'DESC',
// 'return' => 'ids',
//);
//$orders = wc_get_orders($args);
//print_r('WC_Order_Query => ');
//print_r($orders);
//$query = new WC_Order_Query(array(
// 'limit' => 100,
// 'page' => 1,
// 'customer_id' => 7,
//// 'offset' => $offset,
// 'order' => 'DESC',
//// 'status' => $status
//));
//$order_list = [];
//$orders = $query->get_orders();
//print_r($orders);
// Getting current customer orders
//$customer_orders = wc_get_orders(array(
// 'meta_key' => '_customer_user',
// 'meta_value' => $current_id,
//// 'post_status' => $order_statuses,
// 'numberposts' => -1
//));
//
//print_r('$customer_orders => ');
//print_r($customer_orders);
//$order = wc_get_order( 197 );
//print_r('$order => ');
//print_r($order);
foreach ($orders as $key => $value) {
$ord = new stdClass();
$ord->ID = $value->ID;
$ord->date = $value->get_date_created()->date_i18n('Y-m-d');
$ord->status = $value->get_status();
$a_status = [
'pending' => '待支付',
'processing' => '已支付(處理中...)',
'complete' => '已完成'
];
$ord->status = $a_status[$ord->status];
$ord->total = $value->total;
$ord->shipping_total = $value->shipping_total;
if ($value->needs_payment()) {
$ord->needs_payment = 1;
} else {
$ord->needs_payment = 0;
}
$count = 0;
$products = [];
foreach ($value->get_items('line_item') as $val) {
$count += $val->get_quantity('view');
$item = new stdClass();
$item->id = $val->get_product_id('view');
$item->quantity = $val->get_quantity('view');
$product = wc_get_product($item->id);
$item->product_name = $product->get_name('view');
$item->product_price = $product->get_price();
$imageId = $product->get_image_id();
$image = wp_get_attachment_image_src($imageId, "full")[0];
$item->img_url = $image;
array_push($products, $item);
}
$ord->count = $count;
$ord->products = $products;
array_push($order_list, $ord);
}
$res = $order_list;
//根据用户id获取购买订单
function get_order_by_user()
{
global $woocommerce;
$customer_id = intval($_REQUEST['uid']);
$status = $_POST['status'];
$page = $_POST['ap'];
$offset = ($page - 1) * 10;
$query = new WC_Order_Query(array(
'limit' => 100,
'page' => $page,
'customer_id' => $customer_id,
'offset' => $offset,
'order' => 'DESC',
'status' => $status
));
$order_list = [];
$orders = $query->get_orders();
foreach ($orders as $key => $value) {
$ord = new stdClass();
$ord->ID = $value->ID;
$ord->date = $value->get_date_created()->date_i18n('Y-m-d');
$ord->status = $value->get_status();
$ord->total = $value->total;
$ord->shipping_total = $value->shipping_total;
if ($value->needs_payment()) {
$ord->needs_payment = 1;
} else {
$ord->needs_payment = 0;
}
// $ord->payment_link = $value->get_checkout_payment_url();
$count = 0;
$products = [];
foreach ($value->get_items('line_item') as $val) {
$count += $val->get_quantity('view');
$item = new stdClass();
$item->id = $val->get_product_id('view');
$item->quantity = $val->get_quantity('view');
$product = wc_get_product($item->id);
$item->product_name = $product->get_name('view');
$item->product_price = $product->get_price();
$imageId = $product->get_image_id();
$image = wp_get_attachment_image_src($imageId, "full")[0];
$item->img_url = $image;
array_push($products, $item);
}
$ord->count = $count;
$ord->products = $products;
array_push($order_list, $ord);
}
// $this->result->rc = 1;
// $this->result->msg = $order_list;
}