$roles )); return $users; } function zacctmgr_get_manager_id($user_id){ return (int)get_the_author_meta('zacctmgr_assigned', $user_id); } function zacctmgr_set_manager_id($user_id, $manager_id){ update_user_meta($user_id, 'zacctmgr_assigned', $manager_id); } function zacctmgr_get_roles(){ // Get all roles global $wp_roles; $all_roles = $wp_roles->roles; $editable_roles = apply_filters('editable_roles', $all_roles); return $editable_roles; } function zacctmgr_get_selected_roles(){ // Get selected roles return get_option('zacctmgr_selected_roles', ['administrator', 'shop_manager']); } function zacctmgr_get_allowed_wc_statuses(){ return get_option('zacctmgr_allowed_woo_statuses', ['wc-processing', 'wc-completed']); } function zacctmgr_get_allowed_no_manager(){ return get_option('zacctmgr_allowed_no', 0); } function zacctmgr_get_default_manager(){ // Get default manager return get_option('zacctmgr_default', 0); } function zacctmgr_get_top_customers($limit = 5){ $users = get_users(array( 'role__not_in' => ['administrator', 'shop_manager'] )); $items = []; if($users){ foreach($users as $user){ $user->total = (float)(wc_get_customer_total_spent($user->ID)); $items[] = $user; } } if(count($items) > 1){ for($i = 0; $i < count($items) - 1; $i++){ for($j = $i + 1; $j < count($items); $j++){ if($items[$i]->total < $items[$j]->total){ $temp = $items[$i]; $items[$i] = $items[$j]; $items[$j] = $temp; } } } } if(count($items) > $limit) $items = array_slice($items, 0, $limit); return $items; } function zacctmgr_get_top_managers($limit = 10){ $users = get_users(array( 'role__not_in' => ['administrator', 'shop_manager'] )); $items = []; if($users){ foreach($users as $user){ $total = (float)(wc_get_customer_total_spent($user->ID)); $manager_id = zacctmgr_get_manager_id($user->ID); $key = 'manager_'.$manager_id; if(isset($items[$key])){ $items[$key]['total'] += $total; $items[$key]['child']++; }else{ $manager_data = get_user_by('ID', $manager_id); if(!$manager_data) continue; $items[$key]['total'] = $total; $items[$key]['child'] = 1; } } } $newItems = []; if(count($items) > 0){ foreach($items as $key => $value){ $key = str_replace('manager_', '', $key); $manager_id = (int)$key; $object = new stdClass; $object->ID = $manager_id; $object->total = $value['total']; $object->child = $value['child']; $newItems[] = $object; } } if(count($newItems) > 1){ for($i = 0; $i < count($newItems) - 1; $i++){ for($j = $i + 1; $j < count($newItems); $j++){ if($newItems[$i]->total < $newItems[$j]->total){ $temp = $newItems[$i]; $newItems[$i] = $newItems[$j]; $newItems[$j] = $temp; } } } } if(count($newItems) > $limit) $newItems = array_slice($newItems, 0, $limit); return $newItems; } function zacctmgr_get_managers_query_by_key($search = ''){ $current_page = $offset = 0; $per_page = -1; $query = new WP_User_Query( array( 'number' => $per_page, 'offset' => $offset, 'role__in' => zacctmgr_get_selected_roles(), 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'first_name', 'value' => $search, 'compare' => 'LIKE' ), array( 'key' => 'last_name', 'value' => $search, 'compare' => 'LIKE' ) ) ) ); return $query; } function zacctmgr_get_customers_query_by_key($search = ''){ $current_page = $offset = 0; $per_page = -1; $query = new WP_User_Query( array( 'role__not_in' => ['administrator', 'shop_manager'], 'number' => $per_page, 'offset' => $offset, 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'first_name', 'value' => $search, 'compare' => 'LIKE' ), array( 'key' => 'last_name', 'value' => $search, 'compare' => 'LIKE' ), array( 'key' => 'billing_company', 'value' => $search, 'compare' => 'LIKE' ), ) ) ); return $query; } function zacctmgr_get_managers_query($params = []){ $current_page = 0; extract($params); $offset = $current_page != 0?($current_page - 1) * $per_page:0; $query = new WP_User_Query( array( 'role__in' => zacctmgr_get_selected_roles(), 'number' => $per_page, 'offset' => $offset ) ); return $query; } function zacctmgr_get_customers_query($params = array(), $orderby = '', $order = ''){ $current_page = $manager_id = 0; $per_page = -1; extract($params); $offset = $current_page != 0?($current_page - 1) * $per_page:0; $final = [ 'role__not_in' => ['administrator', 'shop_manager'] ]; if($orderby == 'spent'){ $final['meta_key'] = '_money_spent'; $final['orderby'] = 'meta_value_num'; $final['order'] = $order; }elseif($orderby == 'accountmanager'){ } if($manager_id != 0){ $final['meta_query'] = [ 'relation' => 'AND', [ 'key' => 'zacctmgr_assigned', 'value' => $manager_id ] ]; } $final['number'] = $per_page; $final['offset'] = $offset; $query = new WP_User_Query($final); return $query; } function zacctmgr_get_customer_list_by_manager($manager_id){ global $wpdb; $prefix = $wpdb->prefix; $user_table = $prefix.'users'; $meta_table = $prefix.'usermeta'; $query = "select user_id from ".$meta_table." where meta_key = 'zacctmgr_assigned' and meta_value = '$manager_id'"; $results = $wpdb->get_results($query); $customers = []; if($results){ foreach($results as $result) $customers[] = (int)$result->user_id; } return $customers; } function zacctmgr_get_total_commission_by_managers($managers){ $data = null; $total_new = $total_existing = 0; if($managers && count($managers) > 0){ foreach($managers as $manager){ $temp = zacctmgr_get_total_commission_by_manager($manager); $data['managers'][$manager->ID] = $temp; $total_new += $temp['total']['new']; $total_existing += $temp['total']['existing']; } } $data['total']['new'] = $total_new; $data['total']['existing'] = $total_existing; $data['total']['total'] = $total_new + $total_existing; return $data; } function zacctmgr_get_total_commission_by_order($order){ $order_id = (int)$order->get_id(); $customer = $order->get_user(); $status = $order->get_status(); $allowed_statuses = zacctmgr_get_allowed_wc_statuses(); if(!in_array('wc-'.$status, $allowed_statuses)) return 0; $data = 0; if(!$customer) return $data; /* Customer level options */ $customer_order_count = (isset($customer->zacctmgr_commission_order_count) && (int)$customer->zacctmgr_commission_order_count > 0)?(int)$customer->zacctmgr_commission_order_count:1; $customer_commission_type = $customer->zacctmgr_commission_type?$customer->zacctmgr_commission_type:'order_level'; $customer_new_value = trim($customer->zacctmgr_commission_new_value); $customer_new_type = trim($customer->zacctmgr_commission_new_type); $customer_new_exclude_options = []; if($customer->zacctmgr_commission_new_exclude_options) $customer_new_exclude_options = $customer->zacctmgr_commission_new_exclude_options; $customer_existing_value = trim($customer->zacctmgr_commission_existing_value); $customer_existing_type = trim($customer->zacctmgr_commission_existing_type); $customer_existing_exclude_options = []; if($customer->zacctmgr_commission_existing_exclude_options) $customer_existing_exclude_options = $customer->zacctmgr_commission_existing_exclude_options; /* Customer level options end */ $manager_id = zacctmgr_get_manager_id($customer->ID); $manager = get_user_by('ID', $manager_id); if($manager){ /* Manager level options */ $manager_order_count = (isset($manager->zacctmgr_commission_order_count) && (int)$manager->zacctmgr_commission_order_count > 0)?(int)$manager->zacctmgr_commission_order_count:1; $manager_commission_type = $manager->zacctmgr_commission_type?$manager->zacctmgr_commission_type:'order_level'; $new_value = trim($manager->zacctmgr_commission_new_value); $new_type = trim($manager->zacctmgr_commission_new_type); $new_exclude_options = []; if($manager->zacctmgr_commission_new_exclude_options) $new_exclude_options = $manager->zacctmgr_commission_new_exclude_options; $existing_value = trim($manager->zacctmgr_commission_existing_value); $existing_type = trim($manager->zacctmgr_commission_existing_type); $existing_exclude_options = []; if($manager->zacctmgr_commission_existing_exclude_options) $existing_exclude_options = $manager->zacctmgr_commission_existing_exclude_options; /* Manager level options end */ if($manager_commission_type == 'no_commission'){ $data = 0; }else{ $orders = null; if($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'no_commission'){ $data = 0; }elseif($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'customer_account_level'){ // Customer level $orders = get_posts([ 'numberposts' => $customer_order_count, 'meta_key' => '_customer_user', 'meta_value' => $customer->ID, 'post_type' => wc_get_order_types(), 'post_status' => $allowed_statuses, 'orderby' => 'post_date', 'order' => 'ASC' ]); }else{ // Order level $orders = get_posts([ 'numberposts' => $manager_order_count, 'meta_key' => '_customer_user', 'meta_value' => $customer->ID, 'post_type' => wc_get_order_types(), 'post_status' => $allowed_statuses, 'orderby' => 'post_date', 'order' => 'ASC' ]); } if(!$orders) return $data; $price = (float)$order->get_total(); $tax = (float)$order->get_total_tax(); $coupon = (float)$order->get_total_discount(); $shipping = (float)$order->get_total_shipping(); $shipping_tax = (float)$order->get_shipping_tax(); $isNew = false; if(count($orders) > 0){ foreach($orders as $item){ if($order_id == $item->ID){ $isNew = true; break; } } } if($isNew){ // New /* New Commission */ if($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'customer_account_level'){ if($customer_new_value != '' && $customer_new_type != ''){ $customer_new_value = (float)$customer_new_value; if($customer_new_type == 'fixed'){ $data = $customer_new_value; }else{ if($customer_new_exclude_options){ foreach($customer_new_exclude_options as $key){ $price -= (float)$$key; } } $data = (float)($customer_new_value * $price / 100); } } }else{ if($new_value != '' && $new_type != ''){ $new_value = (float)$new_value; if($new_type == 'fixed'){ $data = $new_value; }else{ if($new_exclude_options){ foreach($new_exclude_options as $key){ $price -= (float)$$key; } } $data = (float)($new_value * $price / 100); } } } /* New Commission End */ }else{ // Existing /* Existing Commission */ if($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'customer_account_level'){ if($customer_existing_value != '' && $customer_existing_type != ''){ $customer_existing_value = (float)$customer_existing_value; if($customer_existing_type == 'fixed'){ $data = $customer_existing_value; }else{ if($customer_existing_exclude_options){ foreach($customer_existing_exclude_options as $key){ $price -= (float)$$key; } } $data = (float)($customer_existing_value * $price / 100); } } }else{ if($existing_value != '' && $existing_type != ''){ $existing_value = (float)$existing_value; if($existing_type == 'fixed'){ $data = $existing_value; }else{ if($existing_exclude_options){ foreach($existing_exclude_options as $key){ $price -= (float)$$key; } } $data = (float)($existing_value * $price / 100); } } } /* Existing Commission End */ } } } return $data; } function zacctmgr_get_total_commission_by_customer($customer){ $data = []; $allowed_statuses = zacctmgr_get_allowed_wc_statuses(); /* Customer level options */ $customer_order_count = (isset($customer->zacctmgr_commission_order_count) && (int)$customer->zacctmgr_commission_order_count > 0)?(int)$customer->zacctmgr_commission_order_count:1; $customer_commission_type = $customer->zacctmgr_commission_type?$customer->zacctmgr_commission_type:'order_level'; $customer_new_value = trim($customer->zacctmgr_commission_new_value); $customer_new_type = trim($customer->zacctmgr_commission_new_type); $customer_new_exclude_options = []; if($customer->zacctmgr_commission_new_exclude_options) $customer_new_exclude_options = $customer->zacctmgr_commission_new_exclude_options; $customer_existing_value = trim($customer->zacctmgr_commission_existing_value); $customer_existing_type = trim($customer->zacctmgr_commission_existing_type); $customer_existing_exclude_options = []; if($customer->zacctmgr_commission_existing_exclude_options) $customer_existing_exclude_options = $customer->zacctmgr_commission_existing_exclude_options; /* Customer level options end */ $manager_id = zacctmgr_get_manager_id($customer->ID); $manager = get_user_by('ID', $manager_id); if($manager){ /* Manager level options */ $manager_order_count = (isset($manager->zacctmgr_commission_order_count) && (int)$manager->zacctmgr_commission_order_count > 0)?(int)$manager->zacctmgr_commission_order_count:1; $manager_commission_type = $manager->zacctmgr_commission_type?$manager->zacctmgr_commission_type:'order_level'; $new_value = trim($manager->zacctmgr_commission_new_value); $new_type = trim($manager->zacctmgr_commission_new_type); $new_exclude_options = []; if($manager->zacctmgr_commission_new_exclude_options) $new_exclude_options = $manager->zacctmgr_commission_new_exclude_options; $existing_value = trim($manager->zacctmgr_commission_existing_value); $existing_type = trim($manager->zacctmgr_commission_existing_type); $existing_exclude_options = []; if($manager->zacctmgr_commission_existing_exclude_options) $existing_exclude_options = $manager->zacctmgr_commission_existing_exclude_options; /* Manager level options end */ if($manager_commission_type == 'no_commission'){ $data = [ 'total' => 0, 'new' => 0, 'existing' => 0 ]; }else{ if($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'no_commission'){ $data = [ 'total' => 0, 'new' => 0, 'existing' => 0 ]; }else{ $new = $existing = 0; $index = $manager_order_count; if($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'customer_account_level'){ $index = $customer_order_count; } $orders = get_posts([ 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => $customer->ID, 'post_type' => wc_get_order_types(), 'post_status' => $allowed_statuses, 'orderby' => 'post_date', 'order' => 'ASC' ]); if($orders && count($orders) > 0){ /* New Commission */ for($i = 0; $i < $index; $i++){ if(!isset($orders[$i])) continue; $order = wc_get_order($orders[$i]->ID); $price = (float)$order->get_total(); $tax = (float)$order->get_total_tax(); $coupon = (float)$order->get_total_discount(); $shipping = (float)$order->get_total_shipping(); $shipping_tax = (float)$order->get_shipping_tax(); if($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'customer_account_level'){ if($customer_new_value != '' && $customer_new_type != ''){ $customer_new_value = (float)$customer_new_value; if($customer_new_type == 'fixed'){ $new += $customer_new_value; }else{ if($customer_new_exclude_options){ foreach($customer_new_exclude_options as $key){ $price -= (float)$$key; } } $new += (float)($customer_new_value * $price / 100); } } }else{ if($new_value != '' && $new_type != ''){ $new_value = (float)$new_value; if($new_type == 'fixed'){ $new += $new_value; }else{ if($new_exclude_options){ foreach($new_exclude_options as $key){ $price -= (float)$$key; } } $new += (float)($new_value * $price / 100); } } } } /* New Commission End */ /* Existing Commission */ if(count($orders) > $index){ for($i = $index; $i < count($orders); $i++){ if(!isset($orders[$i])) continue; $order = wc_get_order($orders[$i]->ID); $price = (float)$order->get_total(); $tax = (float)$order->get_total_tax(); $coupon = (float)$order->get_total_discount(); $shipping = (float)$order->get_total_shipping(); $shipping_tax = (float)$order->get_shipping_tax(); if($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'customer_account_level'){ if($customer_existing_value != '' && $customer_existing_type != ''){ $customer_existing_value = (float)$customer_existing_value; if($customer_existing_type == 'fixed'){ $existing += $customer_existing_value; }else{ if($customer_existing_exclude_options){ foreach($customer_existing_exclude_options as $key){ $price -= (float)$$key; } } $existing += (float)($customer_existing_value * $price / 100); } } }else{ if($existing_value != '' && $existing_type != ''){ $existing_value = (float)$existing_value; if($existing_type == 'fixed'){ $existing += $existing_value; }else{ if($existing_exclude_options){ foreach($existing_exclude_options as $key){ $price -= (float)$$key; } } $existing += (float)($existing_value * $price / 100); } } } } } /* Existing Commission End */ } // Order Check End $data = [ 'total' => $new + $existing, 'new' => $new, 'existing' => $existing ]; } } }else{ $data = [ 'total' => 0, 'new' => 0, 'existing' => 0 ]; } return $data; } function zacctmgr_get_total_commission_by_manager($manager){ $allowed_statuses = zacctmgr_get_allowed_wc_statuses(); $order_count = (isset($manager->zacctmgr_commission_order_count) && (int)$manager->zacctmgr_commission_order_count > 0)?(int)$manager->zacctmgr_commission_order_count:1; $new_value = trim($manager->zacctmgr_commission_new_value); $new_type = trim($manager->zacctmgr_commission_new_type); $new_exclude_options = []; if($manager->zacctmgr_commission_new_exclude_options) $new_exclude_options = $manager->zacctmgr_commission_new_exclude_options; $existing_value = trim($manager->zacctmgr_commission_existing_value); $existing_type = trim($manager->zacctmgr_commission_existing_type); $existing_exclude_options = []; if($manager->zacctmgr_commission_existing_exclude_options) $existing_exclude_options = $manager->zacctmgr_commission_existing_exclude_options; $customers = zacctmgr_get_customer_list_by_manager($manager->ID); if(!$customers || count($customers) == 0) return 0; $data = []; $commission_new = $commission_existing = 0; $manager_commission_type = $manager->zacctmgr_commission_type?$manager->zacctmgr_commission_type:'order_level'; if($manager_commission_type == 'no_commission'){ foreach($customers as $customer_id){ $data['customers'][$customer_id]['new'] = 0; $data['customers'][$customer_id]['existing'] = 0; $data['customers'][$customer_id]['total'] = 0; } $commission_new = $commission_existing = 0; }else{ foreach($customers as $customer_id){ $customer = get_user_by('ID', $customer_id); $customer_order_count = (isset($customer->zacctmgr_commission_order_count) && (int)$customer->zacctmgr_commission_order_count > 0)?(int)$customer->zacctmgr_commission_order_count:1; $customer_commission_type = 'order_level'; if($customer){ $customer_commission_type = $customer->zacctmgr_commission_type?$customer->zacctmgr_commission_type:'order_level'; } /* Customer Level Variables */ $customer_new_value = trim($customer->zacctmgr_commission_new_value); $customer_new_type = trim($customer->zacctmgr_commission_new_type); $customer_new_exclude_options = []; if($customer->zacctmgr_commission_new_exclude_options) $customer_new_exclude_options = $customer->zacctmgr_commission_new_exclude_options; $customer_existing_value = trim($customer->zacctmgr_commission_existing_value); $customer_existing_type = trim($customer->zacctmgr_commission_existing_type); $customer_existing_exclude_options = []; if($customer->zacctmgr_commission_existing_exclude_options) $customer_existing_exclude_options = $customer->zacctmgr_commission_existing_exclude_options; /* Customer Level Variables End */ $new = $existing = 0; if($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'no_commission'){ $data['customers'][$customer_id]['new'] = 0; $data['customers'][$customer_id]['existing'] = 0; $data['customers'][$customer_id]['total'] = 0; continue; } $orders = get_posts([ 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => $customer_id, 'post_type' => wc_get_order_types(), 'post_status' => $allowed_statuses, 'orderby' => 'post_date', 'order' => 'ASC' ]); $index = $order_count; if($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'customer_account_level'){ $index = $customer_order_count; } if($orders && count($orders) > 0){ /* New Commission */ for($i = 0; $i < $index; $i++){ if(!isset($orders[$i])) continue; $order = wc_get_order($orders[$i]->ID); $price = (float)$order->get_total(); $tax = (float)$order->get_total_tax(); $coupon = (float)$order->get_total_discount(); $shipping = (float)$order->get_total_shipping(); $shipping_tax = (float)$order->get_shipping_tax(); if($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'customer_account_level'){ if($customer_new_value != '' && $customer_new_type != ''){ $customer_new_value = (float)$customer_new_value; if($customer_new_type == 'fixed'){ $new += $customer_new_value; }else{ if($customer_new_exclude_options){ foreach($customer_new_exclude_options as $key){ $price -= (float)$$key; } } $new += (float)($customer_new_value * $price / 100); } } }else{ if($new_value != '' && $new_type != ''){ $new_value = (float)$new_value; if($new_type == 'fixed'){ $new += $new_value; }else{ if($new_exclude_options){ foreach($new_exclude_options as $key){ $price -= (float)$$key; } } $new += (float)($new_value * $price / 100); } } } } /* New Commission End */ /* Existing Commission */ if(count($orders) > $index){ for($i = $index; $i < count($orders); $i++){ if(!isset($orders[$i])) continue; $order = wc_get_order($orders[$i]->ID); $price = (float)$order->get_total(); $tax = (float)$order->get_total_tax(); $coupon = (float)$order->get_total_discount(); $shipping = (float)$order->get_total_shipping(); $shipping_tax = (float)$order->get_shipping_tax(); if($manager_commission_type == 'customer_account_level' && $customer_commission_type == 'customer_account_level'){ if($customer_existing_value != '' && $customer_existing_type != ''){ $customer_existing_value = (float)$customer_existing_value; if($customer_existing_type == 'fixed'){ $existing += $customer_existing_value; }else{ if($customer_existing_exclude_options){ foreach($customer_existing_exclude_options as $key){ $price -= (float)$$key; } } $existing += (float)($customer_existing_value * $price / 100); } } }else{ if($existing_value != '' && $existing_type != ''){ $existing_value = (float)$existing_value; if($existing_type == 'fixed'){ $existing += $existing_value; }else{ if($existing_exclude_options){ foreach($existing_exclude_options as $key){ $price -= (float)$$key; } } $existing += (float)($existing_value * $price / 100); } } } } } /* Existing Commission End */ $commission_new += $new; $commission_existing += $existing; } $data['customers'][$customer_id]['new'] = $new; $data['customers'][$customer_id]['existing'] = $existing; $data['customers'][$customer_id]['total'] = $new + $existing; } } $data['total']['new'] = $commission_new; $data['total']['existing'] = $commission_existing; $data['total']['total'] = $commission_new + $commission_existing; return $data; } function zacctmgr_get_manager_report($manager_id){ include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php' ); include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-report-sales-by-date.php' ); $customers = zacctmgr_get_customer_list_by_manager($manager_id); if(!$customers || count($customers) == 0) return null; $report = new stdClass(); $reportObject = new WC_Report_Sales_By_Date(); $reportObject->group_by_query = "YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)"; $report->customers = $customers; $report->detailed_order_items = (array) $reportObject->get_order_report_data( array( 'data' => array( '_qty' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'order_item_count', ), '_product_id' => array( 'type' => 'order_item_meta', 'name' => 'product_id', 'function' => '' ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), 'order_item_name' => array( 'type' => 'order_item', 'function' => '', 'name' => 'order_item_name', ), ), 'where' => array( array( 'key' => 'order_items.order_item_type', 'value' => 'line_item', 'operator' => '=', ), ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customers, 'operator' => 'IN', 'type' => 'parent' ), ), #'group_by' => $reportObject->group_by_query, 'group_by' => 'order_items.order_item_name', 'order_by' => 'order_item_count DESC', 'query_type' => 'get_results', 'filter_range' => false, 'order_types' => wc_get_order_types( 'order-count' ), 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), ) ); $report->order_counts = (array) $reportObject->get_order_report_data( array( 'data' => array( 'ID' => array( 'type' => 'post_data', 'function' => 'COUNT', 'name' => 'count', 'distinct' => true, ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'group_by' => $reportObject->group_by_query, 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_types' => wc_get_order_types( 'order-count' ), 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customers, 'operator' => 'IN', 'type' => 'parent' ), ), ) ); $report->coupons = (array) $reportObject->get_order_report_data( array( 'data' => array( 'order_item_name' => array( 'type' => 'order_item', 'function' => '', 'name' => 'order_item_name', ), 'discount_amount' => array( 'type' => 'order_item_meta', 'order_item_type' => 'coupon', 'function' => 'SUM', 'name' => 'discount_amount', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'where' => array( array( 'key' => 'order_items.order_item_type', 'value' => 'coupon', 'operator' => '=', ), ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customers, 'operator' => 'IN', 'type' => 'parent' ), ), 'group_by' => $reportObject->group_by_query . ', order_item_name', 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_types' => wc_get_order_types( 'order-count' ), 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), ) ); $report->order_items = (array) $reportObject->get_order_report_data( array( 'data' => array( '_qty' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'order_item_count', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'where' => array( array( 'key' => 'order_items.order_item_type', 'value' => 'line_item', 'operator' => '=', ), ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customers, 'operator' => 'IN', 'type' => 'parent' ), ), 'group_by' => $reportObject->group_by_query, 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_types' => wc_get_order_types( 'order-count' ), 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), ) ); $report->refunded_order_items = absint( $reportObject->get_order_report_data( array( 'data' => array( '_qty' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'order_item_count', ), ), 'where' => array( array( 'key' => 'order_items.order_item_type', 'value' => 'line_item', 'operator' => '=', ), ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customers, 'operator' => 'IN', 'type' => 'parent' ), ), 'query_type' => 'get_var', 'filter_range' => false, 'order_types' => wc_get_order_types( 'order-count' ), 'order_status' => array( 'refunded' ), ) ) ); $report->orders = (array) $reportObject->get_order_report_data( array( 'data' => array( '_order_total' => array( 'type' => 'meta', 'function' => 'SUM', 'name' => 'total_sales', ), '_order_shipping' => array( 'type' => 'meta', 'function' => 'SUM', 'name' => 'total_shipping', ), '_order_tax' => array( 'type' => 'meta', 'function' => 'SUM', 'name' => 'total_tax', ), '_order_shipping_tax' => array( 'type' => 'meta', 'function' => 'SUM', 'name' => 'total_shipping_tax', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'group_by' => $reportObject->group_by_query, 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_types' => wc_get_order_types( 'sales-reports' ), 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customers, 'operator' => 'IN', 'type' => 'parent' ), ), ) ); $report->full_refunds = (array) $reportObject->get_order_report_data( array( 'data' => array( '_order_total' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'total_refund', ), '_customer_user' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'customer_user', ), '_order_shipping' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'total_shipping', ), '_order_tax' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'total_tax', ), '_order_shipping_tax' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'total_shipping_tax', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'group_by' => 'posts.post_parent', 'query_type' => 'get_results', 'filter_range' => false, 'order_status' => false, 'parent_order_status' => array( 'refunded' ), 'where' => array( array( 'key' => 'parent_meta__customer_user.meta_value', 'value' => $customers, 'operator' => 'IN' ), ) ) ); $report->partial_refunds = (array) $reportObject->get_order_report_data( array( 'data' => array( 'ID' => array( 'type' => 'post_data', 'function' => '', 'name' => 'refund_id', ), '_customer_user' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'customer_user', ), '_refund_amount' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_refund', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), 'order_item_type' => array( 'type' => 'order_item', 'function' => '', 'name' => 'item_type', 'join_type' => 'LEFT', ), '_order_total' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_sales', ), '_order_shipping' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_shipping', 'join_type' => 'LEFT', ), '_order_tax' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_tax', 'join_type' => 'LEFT', ), '_order_shipping_tax' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_shipping_tax', 'join_type' => 'LEFT', ), '_qty' => array( 'type' => 'order_item_meta', 'function' => 'SUM', 'name' => 'order_item_count', 'join_type' => 'LEFT', ), ), 'group_by' => 'refund_id', 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_status' => false, 'parent_order_status' => array( 'completed', 'processing', 'on-hold' ), 'where' => array( array( 'key' => 'parent_meta__customer_user.meta_value', 'value' => $customers, 'operator' => 'IN' ), ) ) ); $report->refund_lines = (array) $reportObject->get_order_report_data( array( 'data' => array( 'ID' => array( 'type' => 'post_data', 'function' => '', 'name' => 'refund_id', ), '_customer_user' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'customer_user', ), '_refund_amount' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_refund', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), 'order_item_type' => array( 'type' => 'order_item', 'function' => '', 'name' => 'item_type', 'join_type' => 'LEFT', ), '_order_total' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_sales', ), '_order_shipping' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_shipping', 'join_type' => 'LEFT', ), '_order_tax' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_tax', 'join_type' => 'LEFT', ), '_order_shipping_tax' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_shipping_tax', 'join_type' => 'LEFT', ), '_qty' => array( 'type' => 'order_item_meta', 'function' => 'SUM', 'name' => 'order_item_count', 'join_type' => 'LEFT', ), ), 'group_by' => 'refund_id', 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_status' => false, 'parent_order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), 'where' => array( array( 'key' => 'parent_meta__customer_user.meta_value', 'value' => $customers, 'operator' => 'IN' ), ) ) ); $report->total_tax_refunded = 0; $report->total_shipping_refunded = 0; $report->total_shipping_tax_refunded = 0; $report->total_refunds = 0; $refunded_orders = array_merge( $report->partial_refunds, $report->full_refunds ); foreach ( $refunded_orders as $key => $value ) { $report->total_tax_refunded += floatval( $value->total_tax < 0 ? $value->total_tax * -1 : $value->total_tax ); $report->total_refunds += floatval( $value->total_refund ); $report->total_shipping_tax_refunded += floatval( $value->total_shipping_tax < 0 ? $value->total_shipping_tax * -1 : $value->total_shipping_tax ); $report->total_shipping_refunded += floatval( $value->total_shipping < 0 ? $value->total_shipping * -1 : $value->total_shipping ); // Only applies to parial. if ( isset( $value->order_item_count ) ) { $report->refunded_order_items += floatval( $value->order_item_count < 0 ? $value->order_item_count * -1 : $value->order_item_count ); } } // Totals from all orders - including those refunded. Subtract refunded amounts. $report->total_tax = wc_format_decimal( array_sum( wp_list_pluck( $report->orders, 'total_tax' ) ) - $report->total_tax_refunded, 2 ); $report->total_shipping = wc_format_decimal( array_sum( wp_list_pluck( $report->orders, 'total_shipping' ) ) - $report->total_shipping_refunded, 2 ); $report->total_shipping_tax = wc_format_decimal( array_sum( wp_list_pluck( $report->orders, 'total_shipping_tax' ) ) - $report->total_shipping_tax_refunded, 2 ); // Total the refunds and sales amounts. Sales subract refunds. Note - total_sales also includes shipping costs. $report->total_sales = wc_format_decimal( array_sum( wp_list_pluck( $report->orders, 'total_sales' ) ) - $report->total_refunds, 2 ); $report->net_sales = wc_format_decimal( $report->total_sales - $report->total_shipping - max( 0, $report->total_tax ) - max( 0, $report->total_shipping_tax ), 2 ); // Total orders and discounts also includes those which have been refunded at some point $report->total_coupons = number_format( array_sum( wp_list_pluck( $report->coupons, 'discount_amount' ) ), 2, '.', '' ); $report->total_refunded_orders = absint( count( $report->full_refunds ) ); // Total orders in this period, even if refunded. $report->total_orders = absint( array_sum( wp_list_pluck( $report->order_counts, 'count' ) ) ); // Item items ordered in this period, even if refunded. $report->total_items = absint( array_sum( wp_list_pluck( $report->order_items, 'order_item_count' ) ) ); $month = (int)date('m'); $report->avg_order = ceil((float)((int)$report->total_orders / $month)); $report->avg_order_size = (float)($report->total_sales / $month); $report->avg_order_item = ceil((float)((int)$report->order_items / $month)); return $report; } function zacctmgr_get_customer_report($customer_id){ include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php' ); include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-report-sales-by-date.php' ); $report = new stdClass(); $reportObject = new WC_Report_Sales_By_Date(); $reportObject->group_by_query = "YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)"; $report->detailed_order_items = (array) $reportObject->get_order_report_data( array( 'data' => array( '_qty' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'order_item_count', ), '_product_id' => array( 'type' => 'order_item_meta', 'name' => 'product_id', 'function' => '' ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), 'order_item_name' => array( 'type' => 'order_item', 'function' => '', 'name' => 'order_item_name', ), ), 'where' => array( array( 'key' => 'order_items.order_item_type', 'value' => 'line_item', 'operator' => '=', ), ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customer_id, 'operator' => '=', 'type' => 'parent' ), ), #'group_by' => $reportObject->group_by_query, 'group_by' => 'order_items.order_item_name', 'order_by' => 'order_item_count DESC', 'query_type' => 'get_results', 'filter_range' => false, 'order_types' => wc_get_order_types( 'order-count' ), 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), ) ); $report->order_counts = (array) $reportObject->get_order_report_data( array( 'data' => array( 'ID' => array( 'type' => 'post_data', 'function' => 'COUNT', 'name' => 'count', 'distinct' => true, ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'group_by' => $reportObject->group_by_query, 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_types' => wc_get_order_types( 'order-count' ), 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customer_id, 'operator' => '=', 'type' => 'parent' ), ), ) ); $report->order_counts_all = (array) $reportObject->get_order_report_data( array( 'data' => array( 'ID' => array( 'type' => 'post_data', 'function' => 'COUNT', 'name' => 'count', 'distinct' => true, ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'group_by' => $reportObject->group_by_query, 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_types' => wc_get_order_types( 'order-count' ), 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), ) ); $report->coupons = (array) $reportObject->get_order_report_data( array( 'data' => array( 'order_item_name' => array( 'type' => 'order_item', 'function' => '', 'name' => 'order_item_name', ), 'discount_amount' => array( 'type' => 'order_item_meta', 'order_item_type' => 'coupon', 'function' => 'SUM', 'name' => 'discount_amount', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'where' => array( array( 'key' => 'order_items.order_item_type', 'value' => 'coupon', 'operator' => '=', ), ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customer_id, 'operator' => '=', 'type' => 'parent' ), ), 'group_by' => $reportObject->group_by_query . ', order_item_name', 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_types' => wc_get_order_types( 'order-count' ), 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), ) ); $report->order_items = (array) $reportObject->get_order_report_data( array( 'data' => array( '_qty' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'order_item_count', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'where' => array( array( 'key' => 'order_items.order_item_type', 'value' => 'line_item', 'operator' => '=', ), ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customer_id, 'operator' => '=', 'type' => 'parent' ), ), 'group_by' => $reportObject->group_by_query, 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_types' => wc_get_order_types( 'order-count' ), 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), ) ); $report->refunded_order_items = absint( $reportObject->get_order_report_data( array( 'data' => array( '_qty' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'order_item_count', ), ), 'where' => array( array( 'key' => 'order_items.order_item_type', 'value' => 'line_item', 'operator' => '=', ), ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customer_id, 'operator' => '=', 'type' => 'parent' ), ), 'query_type' => 'get_var', 'filter_range' => false, 'order_types' => wc_get_order_types( 'order-count' ), 'order_status' => array( 'refunded' ), ) ) ); $report->orders = (array) $reportObject->get_order_report_data( array( 'data' => array( '_order_total' => array( 'type' => 'meta', 'function' => 'SUM', 'name' => 'total_sales', ), '_order_shipping' => array( 'type' => 'meta', 'function' => 'SUM', 'name' => 'total_shipping', ), '_order_tax' => array( 'type' => 'meta', 'function' => 'SUM', 'name' => 'total_tax', ), '_order_shipping_tax' => array( 'type' => 'meta', 'function' => 'SUM', 'name' => 'total_shipping_tax', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'group_by' => $reportObject->group_by_query, 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_types' => wc_get_order_types( 'sales-reports' ), 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), 'where_meta' => array( array( 'meta_key' => '_customer_user', 'meta_value' => $customer_id, 'operator' => '=', 'type' => 'parent' ), ), ) ); $report->full_refunds = (array) $reportObject->get_order_report_data( array( 'data' => array( '_order_total' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'total_refund', ), '_customer_user' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'customer_user', ), '_order_shipping' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'total_shipping', ), '_order_tax' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'total_tax', ), '_order_shipping_tax' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'total_shipping_tax', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), ), 'group_by' => 'posts.post_parent', 'query_type' => 'get_results', 'filter_range' => false, 'order_status' => false, 'parent_order_status' => array( 'refunded' ), 'where' => array( array( 'key' => 'parent_meta__customer_user.meta_value', 'value' => $customer_id, 'operator' => '=' ), ) ) ); $report->partial_refunds = (array) $reportObject->get_order_report_data( array( 'data' => array( 'ID' => array( 'type' => 'post_data', 'function' => '', 'name' => 'refund_id', ), '_customer_user' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'customer_user', ), '_refund_amount' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_refund', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), 'order_item_type' => array( 'type' => 'order_item', 'function' => '', 'name' => 'item_type', 'join_type' => 'LEFT', ), '_order_total' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_sales', ), '_order_shipping' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_shipping', 'join_type' => 'LEFT', ), '_order_tax' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_tax', 'join_type' => 'LEFT', ), '_order_shipping_tax' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_shipping_tax', 'join_type' => 'LEFT', ), '_qty' => array( 'type' => 'order_item_meta', 'function' => 'SUM', 'name' => 'order_item_count', 'join_type' => 'LEFT', ), ), 'group_by' => 'refund_id', 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_status' => false, 'parent_order_status' => array( 'completed', 'processing', 'on-hold' ), 'where' => array( array( 'key' => 'parent_meta__customer_user.meta_value', 'value' => $customer_id, 'operator' => '=' ), ) ) ); $report->refund_lines = (array) $reportObject->get_order_report_data( array( 'data' => array( 'ID' => array( 'type' => 'post_data', 'function' => '', 'name' => 'refund_id', ), '_customer_user' => array( 'type' => 'parent_meta', 'function' => '', 'name' => 'customer_user', ), '_refund_amount' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_refund', ), 'post_date' => array( 'type' => 'post_data', 'function' => '', 'name' => 'post_date', ), 'order_item_type' => array( 'type' => 'order_item', 'function' => '', 'name' => 'item_type', 'join_type' => 'LEFT', ), '_order_total' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_sales', ), '_order_shipping' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_shipping', 'join_type' => 'LEFT', ), '_order_tax' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_tax', 'join_type' => 'LEFT', ), '_order_shipping_tax' => array( 'type' => 'meta', 'function' => '', 'name' => 'total_shipping_tax', 'join_type' => 'LEFT', ), '_qty' => array( 'type' => 'order_item_meta', 'function' => 'SUM', 'name' => 'order_item_count', 'join_type' => 'LEFT', ), ), 'group_by' => 'refund_id', 'order_by' => 'post_date ASC', 'query_type' => 'get_results', 'filter_range' => false, 'order_status' => false, 'parent_order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ), 'where' => array( array( 'key' => 'parent_meta__customer_user.meta_value', 'value' => $customer_id, 'operator' => '=' ), ) ) ); $report->total_tax_refunded = 0; $report->total_shipping_refunded = 0; $report->total_shipping_tax_refunded = 0; $report->total_refunds = 0; $refunded_orders = array_merge( $report->partial_refunds, $report->full_refunds ); foreach ( $refunded_orders as $key => $value ) { $report->total_tax_refunded += floatval( $value->total_tax < 0 ? $value->total_tax * -1 : $value->total_tax ); $report->total_refunds += floatval( $value->total_refund ); $report->total_shipping_tax_refunded += floatval( $value->total_shipping_tax < 0 ? $value->total_shipping_tax * -1 : $value->total_shipping_tax ); $report->total_shipping_refunded += floatval( $value->total_shipping < 0 ? $value->total_shipping * -1 : $value->total_shipping ); // Only applies to parial. if ( isset( $value->order_item_count ) ) { $report->refunded_order_items += floatval( $value->order_item_count < 0 ? $value->order_item_count * -1 : $value->order_item_count ); } } // Totals from all orders - including those refunded. Subtract refunded amounts. $report->total_tax = wc_format_decimal( array_sum( wp_list_pluck( $report->orders, 'total_tax' ) ) - $report->total_tax_refunded, 2 ); $report->total_shipping = wc_format_decimal( array_sum( wp_list_pluck( $report->orders, 'total_shipping' ) ) - $report->total_shipping_refunded, 2 ); $report->total_shipping_tax = wc_format_decimal( array_sum( wp_list_pluck( $report->orders, 'total_shipping_tax' ) ) - $report->total_shipping_tax_refunded, 2 ); // Total the refunds and sales amounts. Sales subract refunds. Note - total_sales also includes shipping costs. $report->total_sales = wc_format_decimal( array_sum( wp_list_pluck( $report->orders, 'total_sales' ) ) - $report->total_refunds, 2 ); $report->net_sales = wc_format_decimal( $report->total_sales - $report->total_shipping - max( 0, $report->total_tax ) - max( 0, $report->total_shipping_tax ), 2 ); // Total orders and discounts also includes those which have been refunded at some point $report->total_coupons = number_format( array_sum( wp_list_pluck( $report->coupons, 'discount_amount' ) ), 2, '.', '' ); $report->total_refunded_orders = absint( count( $report->full_refunds ) ); // Total orders in this period, even if refunded. $report->total_orders = absint( array_sum( wp_list_pluck( $report->order_counts, 'count' ) ) ); $report->total_orders_all = absint( array_sum( wp_list_pluck( $report->order_counts_all, 'count' ) ) ); // Item items ordered in this period, even if refunded. $report->total_items = absint( array_sum( wp_list_pluck( $report->order_items, 'order_item_count' ) ) ); $month = (int)date('m'); $report->avg_order = ceil((float)((int)$report->total_orders / $month)); $report->avg_order_size = (float)($report->total_sales / $month); $report->avg_order_item = ceil((float)((int)$report->order_items / $month)); return $report; } function zacctmgr_get_product_category_by_id($product_id){ $terms = get_the_terms($product_id, 'product_cat'); $product_categories = []; if($terms){ foreach($terms as $term){ if($term->name == 'Uncategorized') continue; $product_categories[] = $term->name; } } return $product_categories; } function zacctmgr_get_orders_by_customers($customers = []){ $customer_orders = get_posts([ 'numberposts' => 3, 'meta_key' => '_customer_user', 'meta_value' => $customers, 'operator' => 'IN', 'post_type' => wc_get_order_types(), 'post_status' => array_keys(wc_get_order_statuses()), 'orderby' => 'post_date', 'order' => 'DESC' ]); return $customer_orders; } function zacctmgr_get_orders_by_customer($customer_id){ $customer_orders = get_posts([ 'numberposts' => 3, 'meta_key' => '_customer_user', 'meta_value' => $customer_id, 'post_type' => wc_get_order_types(), 'post_status' => array_keys(wc_get_order_statuses()), 'orderby' => 'post_date', 'order' => 'DESC' ]); return $customer_orders; } ?>