'customer', 'plural' => 'customers', 'ajax' => false, ) ); } public function print_overview() { $this->prepare_items(); echo '
'; echo '

Account Manager Overview

'; echo '
'; echo '

Account Manager List

'; if ( ! empty( $_GET['link_orders'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'link_orders' ) ) { $linked = wc_update_new_customer_past_orders( absint( $_GET['link_orders'] ) ); echo '

' . sprintf( _n( '%s previous order linked', '%s previous orders linked', $linked, 'woocommerce' ), $linked ) . '

'; } if ( ! empty( $_GET['refresh'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'refresh' ) ) { $user_id = absint( $_GET['refresh'] ); $user = get_user_by( 'id', $user_id ); delete_user_meta( $user_id, '_money_spent' ); delete_user_meta( $user_id, '_order_count' ); echo '

' . sprintf( __( 'Refreshed stats for %s', 'woocommerce' ), $user->display_name ) . '

'; } if ( zacctmgr_allow_edit_others_commission() ) { $manager_id = 0; } else { $manager_id = get_current_user_id(); } $query = zacctmgr_get_customers_query( array( 'manager_id' => $manager_id, 'per_page' => - 1 ) ); echo ''; if ( ! isset( $_REQUEST['manager_filter'] ) ) { echo '
'; echo ''; echo ''; submit_button( 'Search Customer', '', 'submit', false, array( 'id' => 'zacctmgr_customer_search_submit' ) ); echo '
'; } $this->display(); echo '
'; } public function column_default( $user, $column_name ) { switch ( $column_name ) { case 'customer_name': if ( $user->last_name && $user->first_name ) { return $user->last_name . ', ' . $user->first_name; } else { return ' - '; } case 'billing_company': $billing_company = get_user_meta( $user->ID, 'billing_company', true ); return $billing_company; case 'location': $state_code = get_user_meta( $user->ID, 'billing_state', true ); $country_code = get_user_meta( $user->ID, 'billing_country', true ); $state = isset( WC()->countries->states[ $country_code ][ $state_code ] ) ? WC()->countries->states[ $country_code ][ $state_code ] : $state_code; $country = isset( WC()->countries->countries[ $country_code ] ) ? WC()->countries->countries[ $country_code ] : $country_code; $value = ''; if ( $state ) { $value .= $state . ', '; } $value .= $country; if ( $value ) { return $value; } else { return ' - '; } case 'email': $billing_phone = get_user_meta( $user->ID, 'billing_phone', true ); return ' ' . $user->user_email . '
' . $billing_phone; case 'accountmanager': $manager_name = ''; $manager_id = zacctmgr_get_manager_id( $user->ID ); if ( $manager_id != 0 ) { $first_name = get_user_meta( $manager_id, 'first_name', true ); $last_name = get_user_meta( $manager_id, 'last_name', true ); if ( $first_name == '' && $last_name == '' ) { return ' - '; } else { return $first_name . ' ' . $last_name; } } return $manager_name; case 'spent': return wc_price( wc_get_customer_total_spent( $user->ID ) ); case 'commissions': $data = zacctmgr_get_total_commission_by_customer( $user ); return wc_price( $data['total'] ); case 'orders': return wc_get_customer_order_count( $user->ID ); case 'last_order': $orders = wc_get_orders( array( 'limit' => 1, 'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ), 'customer' => $user->ID, ) ); if ( ! empty( $orders ) ) { $order = $orders[0]; return '' . _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() . ' – ' . wc_format_datetime( $order->get_date_created() ); } else { return '-'; } break; case 'wc_actions': ob_start(); ?>

wp_nonce_url( add_query_arg( 'refresh', $user->ID ), 'refresh' ), 'name' => 'Refresh stats', 'action' => 'refresh', ); $actions['edit'] = array( 'url' => admin_url( 'user-edit.php?user_id=' . $user->ID ), 'name' => 'Edit', 'action' => 'edit', ); $actions['view'] = array( 'url' => admin_url( 'edit.php?post_type=shop_order&_customer_user=' . $user->ID ), 'name' => 'View orders', 'action' => 'view', ); $orders = wc_get_orders( array( 'limit' => 1, 'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ), 'customer' => array( array( 0, $user->user_email ) ), ) ); if ( $orders ) { $actions['link'] = array( 'url' => wp_nonce_url( add_query_arg( 'link_orders', $user->ID ), 'link_orders' ), 'name' => 'Link previous orders', 'action' => 'link', ); } $actions['insight'] = array( 'url' => admin_url( 'admin.php?page=zacctmgr_insights&customer_id=' . $user->ID ), 'name' => 'View insights', 'action' => 'insight', ); $actions = apply_filters( 'woocommerce_admin_user_actions', $actions, $user ); foreach ( $actions as $action ) { printf( '%s', esc_attr( $action['action'] ), esc_url( $action['url'] ), esc_attr( $action['name'] ), esc_attr( $action['name'] ) ); } do_action( 'woocommerce_admin_user_actions_end', $user ); ?>

'Name (Last, First)', 'billing_company' => 'Company', 'email' => 'Contact', 'accountmanager' => 'Account Manager', 'orders' => 'Orders', 'spent' => 'Money spent', 'commissions' => 'Commissions', 'last_order' => 'Last order', 'wc_actions' => 'Actions', ); if ( ! zacctmgr_allow_edit_commission() ) { unset( $columns['commissions'] ); } return $columns; } public function get_sortable_columns() { return array( 'accountmanager' => array( 'accountmanager', true ), 'spent' => array( 'spent', true ), //'commissions' => array('commissions', true) ); } public function order_by_last_name( $query ) { global $wpdb; $s = ! empty( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : ''; $orderby = ! empty( $_REQUEST['orderby'] ) ? stripslashes( $_REQUEST['orderby'] ) : ''; $order = ! empty( $_REQUEST['order'] ) ? stripslashes( $_REQUEST['order'] ) : ''; if ( $orderby == '' || $order == '' ) { $query->query_from .= " LEFT JOIN {$wpdb->usermeta} as meta2 ON ({$wpdb->users}.ID = meta2.user_id) "; $query->query_where .= " AND meta2.meta_key = 'last_name' "; $query->query_orderby = ' ORDER BY meta2.meta_value, user_login ASC'; } else { if ( $orderby == 'spent' ) { $query->query_from .= " LEFT JOIN {$wpdb->usermeta} as meta2 on ({$wpdb->users}.ID = meta2.user_id) "; $query->query_where .= " AND meta2.meta_key = '_money_spent' "; $query->query_orderby = ' ORDER BY meta2.meta_value + 0 ' . $order; } elseif ( $orderby == 'accountmanager' ) { $query->query_from .= " LEFT JOIN {$wpdb->usermeta} as meta2 on ({$wpdb->users}.ID = meta2.user_id AND meta2.meta_key='zacctmgr_assigned') "; $query->query_from .= " LEFT JOIN {$wpdb->users} as meta4 on (meta2.meta_value = meta4.ID) "; $query->query_orderby = " ORDER BY meta4.display_name " . $order; } } if ( $s ) { $query->query_from .= " LEFT JOIN {$wpdb->usermeta} as meta3 ON ({$wpdb->users}.ID = meta3.user_id)"; $query->query_where .= " AND ( user_login LIKE '%" . esc_sql( str_replace( '*', '', $s ) ) . "%' OR user_nicename LIKE '%" . esc_sql( str_replace( '*', '', $s ) ) . "%' OR meta3.meta_value LIKE '%" . esc_sql( str_replace( '*', '', $s ) ) . "%' ) "; $query->query_orderby = ' GROUP BY ID ' . $query->query_orderby; } return $query; } public function usort_reorder( $a, $b ) { $orderby = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'accountmanager'; $order = ( ! empty( $_GET['order'] ) ) ? $_GET['order'] : 'asc'; if ( $orderby == 'accountmanager' ) { /* A Name */ $manager_id_a = zacctmgr_get_manager_id( $a->ID ); $name_a = ''; if ( $manager_id_a != 0 ) { $first_name_a = get_user_meta( $manager_id_a, 'first_name', true ); $last_name_a = get_user_meta( $manager_id_a, 'last_name', true ); if ( $first_name_a != '' || $last_name_a != '' ) { $name_a = $first_name_a . ' ' . $last_name_a; } } /* A Name End */ /* B Name */ $manager_id_b = zacctmgr_get_manager_id( $b->ID ); $name_b = ''; if ( $manager_id_b != 0 ) { $first_name_b = get_user_meta( $manager_id_b, 'first_name', true ); $last_name_b = get_user_meta( $manager_id_b, 'last_name', true ); if ( $first_name_b != '' || $last_name_b != '' ) { $name_b = $first_name_b . ' ' . $last_name_b; } } /* B Name End */ $result = strcmp( $name_a, $name_b ) ? 1 : - 1; return ( $order === 'asc' ) ? $result : - $result; } elseif ( $orderby == 'spent' ) { $spent_a = (float) wc_get_customer_total_spent( $a->ID ); $spent_b = (float) wc_get_customer_total_spent( $b->ID ); $result = $spent_a >= $spent_b ? 1 : - 1; return ( $order === 'asc' ) ? $result : - $result; } elseif ( $orderby == 'commissions' ) { $data1 = zacctmgr_get_total_commission_by_customer( $a ); $data2 = zacctmgr_get_total_commission_by_customer( $b ); $result = $data1['total'] >= $data2['total'] ? 1 : - 1; return ( $order === 'asc' ) ? $result : - $result; } else { return 1; } } public function extra_tablenav( $which ) { if ( $which == 'top' ) { $manager_id = 0; if ( isset( $_REQUEST['manager_filter'] ) ) { $manager_id = (int) $_REQUEST['manager_filter']; } $users = zacctmgr_get_em_users(); if ( zacctmgr_allow_edit_others_commission() ): ?>
get_pagenum() ); $per_page = 20; /** * Init column headers. */ $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() ); add_action( 'pre_user_query', array( $this, 'order_by_last_name' ) ); /** * Get users. */ if ( zacctmgr_allow_edit_others_commission() ) { $manager_id = 0; } else { $manager_id = get_current_user_id(); } if ( isset( $_REQUEST['name'] ) ) { $name = esc_textarea( $_REQUEST['name'] ); } else { $name = ''; } if ( isset( $_REQUEST['manager_filter'] ) ) { $manager_id = (int) $_REQUEST['manager_filter']; } $orderby = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'accountmanager'; $order = ( ! empty( $_GET['order'] ) ) ? $_GET['order'] : 'asc'; $query = zacctmgr_get_customers_query( array( 'current_page' => $current_page, 'per_page' => $per_page, 'manager_id' => $manager_id, 'name' => $name ), $orderby, $order ); $results = $query->get_results(); //usort($results, array($this, 'usort_reorder')); $this->items = $results; remove_action( 'pre_user_query', array( $this, 'order_by_last_name' ) ); /** * Pagination. */ $this->set_pagination_args( array( 'total_items' => $query->total_users, 'per_page' => $per_page, 'total_pages' => ceil( $query->total_users / $per_page ), ) ); } } ?>