php - 通过用户 ID 在 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;
}
解决方案
我用这种方法解决了我的需求(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;
}
推荐阅读
- react-native - 我尝试安装弹出对话框以响应本机
- node.js - ENV 变量未从 Docker 传递到节点 JS 代码
- javascript - 如何在javascript函数中将日期作为参数传递
- javascript - 如何在 php 代码中获取 Javascript 函数变量以存储在数据库中
- visual-c++ - 错误:“const char*”类型的参数与“char*”类型的参数不兼容
- sql - SQL Server:左连接下存在的位置
- excel - 如何修复函数“SetRebarColumn”以正确地将参数从 excel 插入 etabs?
- html - 在 div 中自动拟合 markdown 图像
- sql - 出现错误“ORA-00942:表或视图不存在”
- ionic-framework - 如何从数组响应中获取项目列表