gpt4 book ai didi

php - 如何获取用户(客户)在 WooCommerce 中花费的总金额?

转载 作者:行者123 更新时间:2023-12-04 12:58:07 25 4
gpt4 key购买 nike

使用以下短代码,我试图获取用户总花费金额,但它会减慢页面加载速度(6 秒)。
是否可以优化此代码以缩短加载时间?

add_shortcode('woo-total-completed', 'get_user_total_completed');

function get_user_total_completed() {
$total_amount = 0; // Init

$total_completed_orders = wc_get_orders( array(
'limit' => -1,
'status' => 'wc-completed',
) );

foreach( $total_completed_orders as $order) {
$total_amount += $order;
}
return $total_amount;
}

最佳答案

您可以简单地使用 WC_Customer method get_total_spent() 这边走:

add_shortcode('user_total_spent', 'get_user_total_spent');

function get_user_total_spent( $atts ) {
extract( shortcode_atts( array(
'user_id' => get_current_user_id(),
), $atts, 'user_total_spent' ) );

if( $user_id > 0 ) {
$customer = new WC_Customer( $user_id ); // Get WC_Customer Object

$total_spent = $customer->get_total_spent(); // Get total spent amount

return wc_price( $total_spent ); // return formatted total spent amount
}
}

// USAGE: [user_total_spent] or [user_total_spent user_id="118"]
代码位于事件子主题(事件主题)的 function.php 文件中。测试和工作。

关于php - 如何获取用户(客户)在 WooCommerce 中花费的总金额?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63533998/

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