• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Easy Digital Downloads a2z

Easy Digital Downloads a2z

Easy Digital Downloads a2z

  • Home
  • Plugins
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / EDD_API::get_customers() – Process Get Customers API Request

You appear to be a bot. Output may be restricted

Description

Process Get Customers API Request

Usage

$array = EDD_API::get_customers( $customer );

Parameters

$customer
( int ) optional – Customer ID

Returns

array $customers Multidimensional array of the customers

Source

File name: easy-digital-downloads/includes/api/class-edd-api.php
Lines:

1 to 93 of 93
  public function get_customers( $customer = null ) {

    $customer  = is_array( $customer ) ? $customer['customer'] : $customer;
    $customers = array();
    $error     = array();

    if( ! user_can( $this->user_id, 'view_shop_sensitive_data' ) && ! $this->override ) {
      return $customers;
    }

    global $wpdb;

    $paged    = $this->get_paged();
    $per_page = $this->per_page();
    $offset   = $per_page * ( $paged - 1 );

    if( is_numeric( $customer ) ) {
      $field = 'id';
    } elseif ( is_array( $customer ) ) {
      // Checking if search is being done by id, email, user_id fields.
      if ( array_key_exists( 'id', $customer ) ) {
        $field = 'id';
      } elseif ( array_key_exists( 'email', $customer ) ) {
        $field = 'email';
      } elseif ( array_key_exists( 'user_id', $customer ) ) {
        $field = 'user_id';
      }

      $customer = $customer[ $field ];
    } else {
      $field = 'email';
    }

    $customer_query = EDD()->customers->get_customers( array( 'number' => $per_page, 'offset' => $offset, $field => $customer ) );
    $customer_count = 0;

    if( $customer_query ) {

      foreach ( $customer_query as $customer_obj ) {

        $names      = explode( ' ', $customer_obj->name );
        $first_name = ! empty( $names[0] ) ? $names[0] : '';
        $last_name  = '';
        if( ! empty( $names[1] ) ) {
          unset( $names[0] );
          $last_name = implode( ' ', $names );
        }

        $customers['customers'][$customer_count]['info']['id']           = '';
        $customers['customers'][$customer_count]['info']['user_id']      = '';
        $customers['customers'][$customer_count]['info']['username']     = '';
        $customers['customers'][$customer_count]['info']['display_name'] = '';
        $customers['customers'][$customer_count]['info']['customer_id']  = $customer_obj->id;
        $customers['customers'][$customer_count]['info']['first_name']   = $first_name;
        $customers['customers'][$customer_count]['info']['last_name']    = $last_name;
        $customers['customers'][$customer_count]['info']['email']        = $customer_obj->email;

        if ( ! empty( $customer_obj->user_id ) && $customer_obj->user_id > 0 ) {

          $user_data = get_userdata( $customer_obj->user_id );

          // Customer with registered account

          // id is going to get deprecated in the future, user user_id or customer_id instead
          $customers['customers'][$customer_count]['info']['id']           = $customer_obj->user_id;
          $customers['customers'][$customer_count]['info']['user_id']      = $customer_obj->user_id;
          $customers['customers'][$customer_count]['info']['username']     = $user_data->user_login;
          $customers['customers'][$customer_count]['info']['display_name'] = $user_data->display_name;

        }

        $customers['customers'][$customer_count]['stats']['total_purchases'] = $customer_obj->purchase_count;
        $customers['customers'][$customer_count]['stats']['total_spent']     = $customer_obj->purchase_value;
        $customers['customers'][$customer_count]['stats']['total_downloads'] = edd_count_file_downloads_of_customer( $customer_obj->id );

        $customer_count++;

      }

    } elseif( $customer ) {

      $error['error'] = sprintf( __( 'Customer %s not found!', 'easy-digital-downloads' ), $customer );
      return $error;

    } else {

      $error['error'] = __( 'No customers found!', 'easy-digital-downloads' );
      return $error;

    }

    return apply_filters( 'edd_api_customers', $customers, $this );
  }
 

 View on GitHub View on Trac

Published: 26th November 2019 | Last updated: 12th September 2021

Primary Sidebar

Information

Function name: EDD_API::get_customers
Class ref: EDD_API
Plugin ref: Easy Digital Downloads
Version: 2.11.6
Sourcefile: includes/api/class-edd-api.php
File ref: includes/api/class-edd-api.php
Deprecated?: No
API Letters: A,C,E,G

Footer

Easy Digital Downloads a2z
Easy Digital Downloads a2z
Easy Digital Downloads a2z
WordPress 5.9.3
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  edd.wp-a2z.org
© Copyright Easy Digital Downloads a2z 2014-2022. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites