- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用以下短代码,我试图获取用户总花费金额,但它会减慢页面加载速度(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/
我一直在读一本分配给类(class)的书,它提到数组访问需要 O(1) 时间。我意识到这非常快(也许尽可能快),但是如果您有一个循环必须多次引用它,那么分配一个临时变量以在数组中查找值有什么好处吗?或
我一直试图找出为什么这个查询花了这么长时间。以前,它的执行时间约为 150 毫秒到 200 毫秒,但现在需要 25 秒或更长时间。这是从昨晚到今天之间的事。唯一改变的就是将数据添加到表中。 根据下面的
我有一个 ng repeat 重复数据。 - data.image(src)部分为null,src=null的不再重复。 我用一个简单的 ng-if 解决了它。
我有一个包含大量测试的 Laravel 项目。我正在使用 pcov 来计算代码覆盖率,大约需要 4 分钟。但是 pcov 不支持分支覆盖,所以我决定使用 xdebug。 使用 xdebug 测试执行,
我已经被这个问题困扰了一段时间了,我被难住了。 Automapper 需要 4 秒来映射 19 个对象。在我的机器(24GB 内存,3.6Ghz i7)上,该操作应该花费毫秒或纳秒。 这是映射调用。
我有一个包含大量测试的 Laravel 项目。我正在使用 pcov 来计算代码覆盖率,大约需要 4 分钟。但是 pcov 不支持分支覆盖,所以我决定使用 xdebug。 使用 xdebug 测试执行,
我在机器 A 上有一个 java 进程通过 TCP 与机器 B 上的 Tomcat 通信。 TCP 连接(只是 syn-syn/ack 交换)始终需要 100 毫秒的数量级,而 ping 请求需要 1
我做了一项任务,从 sqlserver 获取超过 200 万条记录并将它们填充到 Asp.net GridView 中。 问题是,查询需要超过 2 分钟才能获得记录,而我的查询现在已经完全优化。 当我
我希望将 165 秒变成 2:40 而不是 0:2:45 函数需要能够适应秒值的大小。 我知道有无数种方法可以做到这一点,但我正在寻找一种干净的方法来做到这一点,除了 jQuery 之外没有任何外部库
我是一名优秀的程序员,十分优秀!