Last active
September 18, 2023 13:40
-
-
Save monecchi/b0358070bfa77d9b52451ddd3e91edf4 to your computer and use it in GitHub Desktop.
WooCommerce Customer's Orders Total - Sum of all the current month's orders made by a customer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*********************************************************************************************/ | |
/** Get the total sum (money spent) of orders made by a user with a "complete" status in WooCommerce. This will output the current month's sum of all orders made by the customer. **/ | |
/*********************************************************************************************/ | |
<?php | |
function current_customer_month_count( $user_id=null ) { | |
if ( empty($user_id) ){ | |
$user_id = get_current_user_id(); | |
} | |
// Date calculations to limit the query | |
$today_year = date( 'Y' ); | |
$today_month = date( 'm' ); | |
$day = date( 'd' ); | |
if ($today_month == '01') { | |
$month = '12'; | |
$year = $today_year - 1; | |
} else{ | |
$month = $today_month - 1; | |
$month = sprintf("%02d", $month); | |
$year = $today_year - 1; | |
} | |
// ORDERS FOR LAST 30 DAYS (Time calculations) | |
$now = strtotime('now'); | |
// Set the gap time (here 30 days) | |
$gap_days = 30; | |
$gap_days_in_seconds = 60*60*24*$gap_days; | |
$gap_time = $now - $gap_days_in_seconds; | |
// The query arguments | |
$args = array( | |
// WC orders post type | |
'post_type' => 'shop_order', | |
// Only orders with status "completed" (others common status: 'wc-on-hold' or 'wc-processing') | |
'post_status' => array( 'wc-completed' ), | |
// all posts | |
'numberposts' => -1, | |
// for current user id | |
'meta_key' => '_customer_user', | |
'meta_value' => $user_id, | |
'date_query' => array( | |
//orders published on last 30 days | |
'relation' => 'OR', | |
array( | |
'year' => $today_year, | |
'month' => $today_month, | |
), | |
array( | |
'year' => $year, | |
'month' => $month, | |
), | |
), | |
); | |
// Get all customer orders | |
$customer_orders = get_posts( $args ); | |
$count = 0; | |
$total = 0; | |
$no_orders_message = __('No orders this month.', 'mytheme'); | |
if (!empty($customer_orders)) { | |
$customer_orders_date = array(); | |
// Going through each current customer orders | |
foreach ( $customer_orders as $customer_order ){ | |
// Conveting order dates in seconds | |
$customer_order_date = strtotime($customer_order->post_date); | |
// Only past 30 days orders | |
if ( $customer_order_date > $gap_time ) { | |
$customer_order_date; | |
$order = new WC_Order( $customer_order->ID ); | |
$order_items = $order->get_items(); | |
$total += $order->get_total(); | |
// Going through each current customer items in the order | |
foreach ( $order_items as $order_item ){ | |
$count++; | |
} | |
} | |
} | |
return floatval( preg_replace( '#[^\d.]#', '', $total, $count ) ); | |
} else { | |
return $no_orders_message; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (function_exists('current_customer_month_count')) { ?> | |
<?php echo $customer_current_month_orders = current_customer_month_count(); ?> | |
<?php } ?> |
Where should i insert this code? I'm trying to insert it at woo/my-orders.php at top, but doing nothing, any help?
https://codeshare.io/2EWWvv
Where should i insert this code? I'm trying to insert it at woo/my-orders.php at top, but doing nothing, any help? https://codeshare.io/2EWWvv
The answer is clearly late, but for those who need it: insert the function into function.php, and call the function from the file where you want to display this value.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why not:
date_query '=> array (
'after' => date ('Y-m-d', strtotime ('- 30 days'))
)
)
which immediately gives orders without extra checks in just 30 days