get_order_count() public method

Return the number of orders this customer has.
public get_order_count ( ) : integer
return integer
 /**
  * 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 WC_Customer($id);
     $last_order = $customer->get_last_order();
     $customer_data = array('id' => $customer->get_id(), 'created_at' => $this->server->format_datetime($customer->get_date_created(), false, true), 'email' => $customer->get_email(), 'first_name' => $customer->get_first_name(), 'last_name' => $customer->get_last_name(), 'username' => $customer->get_username(), 'role' => $customer->get_role(), 'last_order_id' => is_object($last_order) ? $last_order->get_id() : null, 'last_order_date' => is_object($last_order) ? $this->server->format_datetime($last_order->get_date_created(), false, true) : null, 'orders_count' => $customer->get_order_count(), 'total_spent' => wc_format_decimal($customer->get_total_spent(), 2), 'avatar_url' => $customer->get_avatar_url(), 'billing_address' => array('first_name' => $customer->get_billing_first_name(), 'last_name' => $customer->get_billing_last_name(), 'company' => $customer->get_billing_company(), 'address_1' => $customer->get_billing_address_1(), 'address_2' => $customer->get_billing_address_2(), 'city' => $customer->get_billing_city(), 'state' => $customer->get_billing_state(), 'postcode' => $customer->get_billing_postcode(), 'country' => $customer->get_billing_country(), 'email' => $customer->get_billing_email(), 'phone' => $customer->get_billing_phone()), 'shipping_address' => array('first_name' => $customer->get_shipping_first_name(), 'last_name' => $customer->get_shipping_last_name(), 'company' => $customer->get_shipping_company(), 'address_1' => $customer->get_shipping_address_1(), 'address_2' => $customer->get_shipping_address_2(), 'city' => $customer->get_shipping_city(), 'state' => $customer->get_shipping_state(), 'postcode' => $customer->get_shipping_postcode(), 'country' => $customer->get_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           $user_data User object.
  * @param WP_REST_Request   $request   Request object.
  * @return WP_REST_Response $response  Response data.
  */
 public function prepare_item_for_response($user_data, $request)
 {
     $customer = new WC_Customer($user_data->ID);
     $data = $customer->get_data();
     $format_date = array('date_created', 'date_modified');
     // Format date values.
     foreach ($format_date as $key) {
         $data[$key] = $data[$key] ? wc_rest_prepare_date_response(get_gmt_from_date(date('Y-m-d H:i:s', $data[$key]))) : null;
     }
     // Remove unwanted CRUD data.
     unset($data['role']);
     // Additional non-crud data.
     $data['last_order'] = null;
     $data['orders_count'] = $customer->get_order_count();
     $data['total_spent'] = $customer->get_total_spent();
     $data['avatar_url'] = $customer->get_avatar_url();
     if ($last_order_data = $customer->get_last_order()) {
         $data['last_order'] = array('id' => $last_order_data->get_id(), 'date' => $last_order_data->get_date_created() ? wc_rest_prepare_date_response($last_order_data->get_date_created()) : null);
     }
     $context = !empty($request['context']) ? $request['context'] : 'view';
     $data = $this->add_additional_fields_to_object($data, $request);
     $data = $this->filter_response_by_context($data, $context);
     $response = rest_ensure_response($data);
     $response->add_links($this->prepare_links($user_data));
     /**
      * Filter customer data returned from the REST API.
      *
      * @param WP_REST_Response $response   The response object.
      * @param WP_User          $user_data  User object used to create response.
      * @param WP_REST_Request  $request    Request object.
      */
     return apply_filters('woocommerce_rest_prepare_customer', $response, $user_data, $request);
 }
 /**
  * Prepare a single customer output for response.
  *
  * @param WP_User           $user_data User object.
  * @param WP_REST_Request   $request   Request object.
  * @return WP_REST_Response $response  Response data.
  */
 public function prepare_item_for_response($user_data, $request)
 {
     $customer = new WC_Customer($user_data->ID);
     $last_order_data = $customer->get_last_order();
     $last_order = null;
     if ($last_order_data) {
         $last_order = array('id' => $last_order_data->get_id(), 'date' => wc_rest_prepare_date_response($last_order_data->get_date_created()));
     }
     $data = array('id' => $customer->get_id(), 'date_created' => wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer->get_date_created())), 'date_modified' => $customer->get_date_modified() ? wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer->get_date_modified())) : null, 'email' => $customer->get_email(), 'first_name' => $customer->get_first_name(), 'last_name' => $customer->get_last_name(), 'username' => $customer->get_username(), 'last_order' => $last_order, 'orders_count' => $customer->get_order_count(), 'total_spent' => $customer->get_total_spent(), 'avatar_url' => $customer->get_avatar_url(), 'billing' => array('first_name' => $customer->get_billing_first_name(), 'last_name' => $customer->get_billing_last_name(), 'company' => $customer->get_billing_company(), 'address_1' => $customer->get_billing_address_1(), 'address_2' => $customer->get_billing_address_2(), 'city' => $customer->get_billing_city(), 'state' => $customer->get_billing_state(), 'postcode' => $customer->get_billing_postcode(), 'country' => $customer->get_billing_country(), 'email' => $customer->get_billing_email(), 'phone' => $customer->get_billing_phone()), 'shipping' => array('first_name' => $customer->get_shipping_first_name(), 'last_name' => $customer->get_shipping_last_name(), 'company' => $customer->get_shipping_company(), 'address_1' => $customer->get_shipping_address_1(), 'address_2' => $customer->get_shipping_address_2(), 'city' => $customer->get_shipping_city(), 'state' => $customer->get_shipping_state(), 'postcode' => $customer->get_shipping_postcode(), 'country' => $customer->get_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($user_data));
     /**
      * Filter customer data returned from the REST API.
      *
      * @param WP_REST_Response $response   The response object.
      * @param WP_User          $user_data  User object used to create response.
      * @param WP_REST_Request  $request    Request object.
      */
     return apply_filters('woocommerce_rest_prepare_customer', $response, $user_data, $request);
 }
/**
 * Get total orders by customer.
 * @param  int $user_id
 * @return int
 */
function wc_get_customer_order_count($user_id)
{
    $customer = new WC_Customer($user_id);
    return $customer->get_order_count();
}
示例#5
0
 /**
  * Test getting a customer's order count from DB.
  * @since 2.7.0
  */
 public function test_customer_get_order_count_read()
 {
     $customer = WC_Helper_Customer::create_customer();
     $customer_id = $customer->get_id();
     WC_Helper_Order::create_order($customer_id);
     WC_Helper_Order::create_order($customer_id);
     WC_Helper_Order::create_order($customer_id);
     $customer = new WC_Customer($customer_id);
     $this->assertEquals(3, $customer->get_order_count());
 }