From Get the customer's total spend for a period of 1 year in WooCommerce answer to my previous question:
// Utility function: Get customer orders total amount from specific status on a period
function get_orders_sum_amount_for( $user_id, $period = '1 year', $status = 'completed' ) {
global $wpdb;
return $wpdb->get_var( $wpdb->prepare( "
SELECT SUM(total_amount)
FROM {$wpdb->prefix}wc_orders
WHERE status = %s
AND customer_id = %d
AND date_created_gmt >= %s
", 'wc-'.$status, $user_id, date('Y-m-d H:i:s', strtotime('-'.$period) ) ) );
}
add_shortcode('user_year_total_spent', 'get_user_year_total_spent');
function get_user_year_total_spent( $atts ) {
extract( shortcode_atts( array(
'user_id' => get_current_user_id(),
), $atts, 'user_year_total_spent' ) );
if( ! $user_id ) return;
$total_spent = get_orders_sum_amount_for( $user_id ); // Get total spent amount
if( $total_spent >= 1200 ) {
$text = __('Congrats you are now tier 3.', 'woocommerce');
} elseif ( $total_spent >= 600 ) {
$text = __('Congrats you are now tier 2.', 'woocommerce');
} else {
$text = __('Congrats you are now tier 1.', 'woocommerce');
}
return sprintf(__('Total Amount Spent: %s. %s', 'woocommerce'), wc_price($total_spent), $text);
}
// SHORTCODE USAGE: [user_year_total_spent]
它接收所有完成的用户订单在一段时间内的总支出.
但我面临的事实是,如果订单部分退回,那么总支出不会减少部分退款的金额.
示例: 订购3件T恤 * 10欧盟. 总支出30欧元.交付费用不包括在总支出中. 此外,该订单中的买方退回了1件T恤 * 10欧元.管理员手动发放了部分退款. 订单状态"已完成",2件T恤 * 20欧盟.但总支出仍为30欧元.
如何从总支出中提取exclude a partial manual refund?