让客户“待命"Woocommerce 中的订单状态总金额 [英] Get customer "on-hold" order status total amount in Woocommerce
问题描述
我正在尝试获取用户在 woocommerce 中保留的所有产品的价格(即用户已下订单但尚未付款).
I'm trying to get the price of all products on hold (i.e user has placed order but haven't made a payment) by a user in woocommerce.
我有以下代码可以检测用户的所有产品暂停订单
I have the following code which detects all products on-hold orders by a user
function get_user_on_hold_product_price() {
global $product, $woocommerce;
// GET USER
$current_user = wp_get_current_user();
// GET USER ON-HOLD ORDERS
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $current_user->ID,
'post_type' => 'shop_order',
'post_status' => 'wc-on-hold',
) );
我不知道该怎么做才能只获得用户所有暂停订单的总价.
I'm not sure what to do from here to get only the total price of all on-hold orders by the user.
将此功能添加/挂钩到短代码,如下所示;
Adding/hooking this function to a shortcode, like this;
add_shortcode('get_on-hold_price', 'get_user_on_hold_product_price')
谢谢
推荐答案
使用 WC_Order_Query
以提高可用性和兼容性:
To get the total amount of customer "on-hold" orders using a WC_Order_Query
for improved usability and compatibility:
add_shortcode('user_on_hold_total', 'get_user_orders_on_hold_total');
function get_user_orders_on_hold_total() {
$total_amount = 0; // Initializing
// Get current user
if( $user = wp_get_current_user() ){
// Get 'on-hold' customer ORDERS
$on_hold_orders = wc_get_orders( array(
'limit' => -1,
'customer_id' => $user->ID,
'status' => 'on-hold',
) );
foreach( $on_hold_orders as $order) {
$total_amount += $order->get_total();
}
}
return $total_amount;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
要获得格式化的总金额,将 return $total_amount;
替换为 return wc_price($total_amount);
To get a formatted total amount replace return $total_amount;
by return wc_price($total_amount);
简码用法:[user_on_hold_total]
相关文档:Woocommerce wc_get_orders()
和WC_Order_Query
Related documentation: Woocommerce wc_get_orders()
and WC_Order_Query
这篇关于让客户“待命"Woocommerce 中的订单状态总金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!