gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:48:22 24 4
gpt4 key购买 nike

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

我正在使用此代码,它当前打印订单值:像这样 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;
}

最佳答案

您可以在下面找到代码,该代码将为您提供一个包含订单值(value)日期和 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
)

)

关于php - 通过用户 ID 在 WooCommerce 中获取单个客户的所有订单和订单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51980040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com