Php 按用户ID获取WooCommerce中单个客户的所有订单和订单数据
我想得到所有的订单金额和付款日期的订单由当前用户在一个插件 我正在使用此代码,它当前打印订单值: 像这样150001000 其中,第一阶为0(从右侧开始),第二阶为100,第三阶为15000。 我试图完成的是将订单数量和日期放入变量中Php 按用户ID获取WooCommerce中单个客户的所有订单和订单数据,php,wordpress,woocommerce,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 = 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;
}
您可以使用下面的代码获取订单日期
$order = new WC_Order($order_id);
$order_date = $order->order_date;
下面您可以找到代码,它将为您提供一个带有订单值日期和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
)
)
我用这种方法解决了我的需求(
wc\u get\u orders
是供应商推荐使用的):
public函数获取已付款订单的总和(int$user\u id):int{
$customer_orders=[];
foreach(wc_get_是_paid_status(),作为$paid_status){
$customer\u orders+=wc\u get\u orders([
'type'=>'shop_order',
“限制”=>-1,
'customer\u id'=>$user\u id,
“状态”=>$PAYED\U状态,
] );
}
$total=0;
foreach($customer\u订单为$order){
$total+=$order->get_total();
//你的密码在这里
}
返回$total;
}
我对woocommerce不是很精通,你能建议我如何编辑上面的代码以数组形式检索日期和金额吗。