/**
  * Get the customer for the given ID
  *
  * @since 2.1
  * @param int $id the customer ID
  * @param array $fields
  * @return array
  */
 public function get_customer($id, $fields = null)
 {
     global $wpdb;
     $id = $this->validate_request($id, 'customer', 'read');
     if (is_wp_error($id)) {
         return $id;
     }
     $customer = new WP_User($id);
     // Get info about user's last order
     $last_order = $wpdb->get_row("SELECT id, post_date_gmt\n\t\t\t\t\t\tFROM {$wpdb->posts} AS posts\n\t\t\t\t\t\tLEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id\n\t\t\t\t\t\tWHERE meta.meta_key = '_customer_user'\n\t\t\t\t\t\tAND   meta.meta_value = {$customer->ID}\n\t\t\t\t\t\tAND   posts.post_type = 'shop_order'\n\t\t\t\t\t\tAND   posts.post_status IN ( '" . implode("','", array_keys(wc_get_order_statuses())) . "' )\n\t\t\t\t\t\tORDER BY posts.ID DESC\n\t\t\t\t\t");
     $customer_data = array('id' => $customer->ID, 'created_at' => $this->server->format_datetime($customer->user_registered), 'last_update' => $this->server->format_datetime(get_user_meta($customer->ID, 'last_update', true)), 'email' => $customer->user_email, 'first_name' => $customer->first_name, 'last_name' => $customer->last_name, 'username' => $customer->user_login, 'role' => $customer->roles[0], 'last_order_id' => is_object($last_order) ? $last_order->id : null, 'last_order_date' => is_object($last_order) ? $this->server->format_datetime($last_order->post_date_gmt) : null, 'orders_count' => wc_get_customer_order_count($customer->ID), 'total_spent' => wc_format_decimal(wc_get_customer_total_spent($customer->ID), 2), 'avatar_url' => $this->get_avatar_url($customer->customer_email), 'billing_address' => array('first_name' => $customer->billing_first_name, 'last_name' => $customer->billing_last_name, 'company' => $customer->billing_company, 'address_1' => $customer->billing_address_1, 'address_2' => $customer->billing_address_2, 'city' => $customer->billing_city, 'state' => $customer->billing_state, 'postcode' => $customer->billing_postcode, 'country' => $customer->billing_country, 'email' => $customer->billing_email, 'phone' => $customer->billing_phone), 'shipping_address' => array('first_name' => $customer->shipping_first_name, 'last_name' => $customer->shipping_last_name, 'company' => $customer->shipping_company, 'address_1' => $customer->shipping_address_1, 'address_2' => $customer->shipping_address_2, 'city' => $customer->shipping_city, 'state' => $customer->shipping_state, 'postcode' => $customer->shipping_postcode, 'country' => $customer->shipping_country));
     return array('customer' => apply_filters('woocommerce_api_customer_response', $customer_data, $customer, $fields, $this->server));
 }
 /**
  * Prepare a single customer output for response.
  *
  * @param WP_User $customer Customer object.
  * @param WP_REST_Request $request Request object.
  * @return WP_REST_Response $response Response data.
  */
 public function prepare_item_for_response($customer, $request)
 {
     $last_order = wc_get_customer_last_order($customer->ID);
     $data = array('id' => $customer->ID, 'date_created' => wc_rest_prepare_date_response($customer->user_registered), 'date_modified' => $customer->last_update ? wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer->last_update)) : null, 'email' => $customer->user_email, 'first_name' => $customer->first_name, 'last_name' => $customer->last_name, 'username' => $customer->user_login, 'last_order' => array('id' => is_object($last_order) ? $last_order->id : null, 'date' => is_object($last_order) ? wc_rest_prepare_date_response($last_order->post->post_date_gmt) : null), 'orders_count' => wc_get_customer_order_count($customer->ID), 'total_spent' => wc_format_decimal(wc_get_customer_total_spent($customer->ID), 2), 'avatar_url' => wc_get_customer_avatar_url($customer->customer_email), 'billing' => array('first_name' => $customer->billing_first_name, 'last_name' => $customer->billing_last_name, 'company' => $customer->billing_company, 'address_1' => $customer->billing_address_1, 'address_2' => $customer->billing_address_2, 'city' => $customer->billing_city, 'state' => $customer->billing_state, 'postcode' => $customer->billing_postcode, 'country' => $customer->billing_country, 'email' => $customer->billing_email, 'phone' => $customer->billing_phone), 'shipping' => array('first_name' => $customer->shipping_first_name, 'last_name' => $customer->shipping_last_name, 'company' => $customer->shipping_company, 'address_1' => $customer->shipping_address_1, 'address_2' => $customer->shipping_address_2, 'city' => $customer->shipping_city, 'state' => $customer->shipping_state, 'postcode' => $customer->shipping_postcode, 'country' => $customer->shipping_country));
     $context = !empty($request['context']) ? $request['context'] : 'view';
     $data = $this->add_additional_fields_to_object($data, $request);
     $data = $this->filter_response_by_context($data, $context);
     // Wrap the data in a response object.
     $response = rest_ensure_response($data);
     $response->add_links($this->prepare_links($customer));
     /**
      * Filter customer data returned from the REST API.
      *
      * @param WP_REST_Response $response  The response object.
      * @param WP_User          $customer  User object used to create response.
      * @param WP_REST_Request  $request   Request object.
      */
     return apply_filters('woocommerce_rest_prepare_customer', $response, $customer, $request);
 }
    /**
     * Get column value.
     *
     * @param WP_User $user
     * @param string $column_name
     * @return string
     */
    public function column_default($user, $column_name)
    {
        global $wpdb;
        switch ($column_name) {
            case 'customer_name':
                if ($user->last_name && $user->first_name) {
                    return $user->last_name . ', ' . $user->first_name;
                } else {
                    return '-';
                }
            case 'username':
                return $user->user_login;
            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':
                return '<a href="mailto:' . $user->user_email . '">' . $user->user_email . '</a>';
            case 'spent':
                return wc_price(wc_get_customer_total_spent($user->ID));
            case 'orders':
                return wc_get_customer_order_count($user->ID);
            case 'last_order':
                $order_ids = get_posts(array('posts_per_page' => 1, 'post_type' => 'shop_order', 'orderby' => 'date', 'order' => 'desc', 'post_status' => array('wc-completed', 'wc-processing'), 'meta_query' => array(array('key' => '_customer_user', 'value' => $user->ID)), 'fields' => 'ids'));
                if ($order_ids) {
                    $order = wc_get_order($order_ids[0]);
                    return '<a href="' . admin_url('post.php?post=' . $order->id . '&action=edit') . '">' . _x('#', 'hash before order number', 'woocommerce') . $order->get_order_number() . '</a> &ndash; ' . date_i18n(get_option('date_format'), strtotime($order->order_date));
                } else {
                    return '-';
                }
                break;
            case 'user_actions':
                ob_start();
                ?>
<p>
					<?php 
                do_action('woocommerce_admin_user_actions_start', $user);
                $actions = array();
                $actions['refresh'] = array('url' => wp_nonce_url(add_query_arg('refresh', $user->ID), 'refresh'), 'name' => __('Refresh stats', 'woocommerce'), 'action' => "refresh");
                $actions['edit'] = array('url' => admin_url('user-edit.php?user_id=' . $user->ID), 'name' => __('Edit', 'woocommerce'), 'action' => "edit");
                $actions['view'] = array('url' => admin_url('edit.php?post_type=shop_order&_customer_user='******'name' => __('View orders', 'woocommerce'), 'action' => "view");
                $order_ids = get_posts(array('posts_per_page' => 1, 'post_type' => wc_get_order_types(), 'post_status' => array_keys(wc_get_order_statuses()), 'meta_query' => array(array('key' => '_customer_user', 'value' => array(0, ''), 'compare' => 'IN'), array('key' => '_billing_email', 'value' => $user->user_email)), 'fields' => 'ids'));
                if ($order_ids) {
                    $actions['link'] = array('url' => wp_nonce_url(add_query_arg('link_orders', $user->ID), 'link_orders'), 'name' => __('Link previous orders', 'woocommerce'), 'action' => "link");
                }
                $actions = apply_filters('woocommerce_admin_user_actions', $actions, $user);
                foreach ($actions as $action) {
                    printf('<a class="button tips %s" href="%s" data-tip="%s">%s</a>', esc_attr($action['action']), esc_url($action['url']), esc_attr($action['name']), esc_attr($action['name']));
                }
                do_action('woocommerce_admin_user_actions_end', $user);
                ?>
				</p><?php 
                $user_actions = ob_get_contents();
                ob_end_clean();
                return $user_actions;
        }
        return '';
    }
 /**
  * Get user from given user ID, email, or login
  *
  * @throws WC_CLI_Exception
  *
  * @since  2.5.0
  * @param  mixed          $id_email_or_login
  * @return array|WP_Error
  */
 protected function get_user($id_email_or_login)
 {
     global $wpdb;
     if (is_numeric($id_email_or_login)) {
         $user = get_user_by('id', $id_email_or_login);
     } else {
         if (is_email($id_email_or_login)) {
             $user = get_user_by('email', $id_email_or_login);
         } else {
             $user = get_user_by('login', $id_email_or_login);
         }
     }
     if (!$user) {
         throw new WC_CLI_Exception('woocommerce_cli_invalid_customer', sprintf(__('Invalid customer "%s"', 'woocommerce'), $id_email_or_login));
     }
     // Get info about user's last order
     $last_order = $wpdb->get_row("SELECT id, post_date_gmt\n\t\t\t\t\t\tFROM {$wpdb->posts} AS posts\n\t\t\t\t\t\tLEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id\n\t\t\t\t\t\tWHERE meta.meta_key = '_customer_user'\n\t\t\t\t\t\tAND   meta.meta_value = {$user->ID}\n\t\t\t\t\t\tAND   posts.post_type = 'shop_order'\n\t\t\t\t\t\tAND   posts.post_status IN ( '" . implode("','", array_keys(wc_get_order_statuses())) . "' )\n\t\t\t\t\t\tORDER BY posts.ID DESC\n\t\t\t\t\t");
     $customer = array('id' => $user->ID, 'created_at' => $this->format_datetime($user->user_registered), 'email' => $user->user_email, 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'username' => $user->user_login, 'role' => $user->roles[0], 'last_order_id' => is_object($last_order) ? $last_order->id : null, 'last_order_date' => is_object($last_order) ? $this->format_datetime($last_order->post_date_gmt) : null, 'orders_count' => wc_get_customer_order_count($user->ID), 'total_spent' => wc_format_decimal(wc_get_customer_total_spent($user->ID), 2), 'avatar_url' => $this->get_avatar_url($user->customer_email), 'billing_address' => array('first_name' => $user->billing_first_name, 'last_name' => $user->billing_last_name, 'company' => $user->billing_company, 'address_1' => $user->billing_address_1, 'address_2' => $user->billing_address_2, 'city' => $user->billing_city, 'state' => $user->billing_state, 'postcode' => $user->billing_postcode, 'country' => $user->billing_country, 'email' => $user->billing_email, 'phone' => $user->billing_phone), 'shipping_address' => array('first_name' => $user->shipping_first_name, 'last_name' => $user->shipping_last_name, 'company' => $user->shipping_company, 'address_1' => $user->shipping_address_1, 'address_2' => $user->shipping_address_2, 'city' => $user->shipping_city, 'state' => $user->shipping_state, 'postcode' => $user->shipping_postcode, 'country' => $user->shipping_country));
     // Allow dot notation for nested array so that user can specifies field
     // like 'billing_address.first_name'.
     return $this->flatten_array($customer);
 }