edd_add_customer_email

Add an email address to the customer from within the admin and log a customer note.

Description

edd_add_customer_email( (array) $args ); 

Parameters (1)

0. $args (array)
Array of arguments: nonce, customer id, and email address

Usage

  1. if ( !function_exists( 'edd_add_customer_email' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'easy-digital-downloads/includes/admin/customers/customer-actions.php'; 
  3.  
  4. // Array of arguments: nonce, customer id, and email address 
  5. $args = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = edd_add_customer_email($args); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/admin/customers/customer-actions.php  
  1. function edd_add_customer_email( $args ) { 
  2.  
  3. $customer_edit_role = apply_filters( 'edd_edit_customers_role', 'edit_shop_payments' ); 
  4.  
  5. if ( ! is_admin() || ! current_user_can( $customer_edit_role ) ) { 
  6. wp_die( __( 'You do not have permission to edit this customer.', 'easy-digital-downloads' ) ); 
  7.  
  8. $output = array(); 
  9.  
  10. if ( empty( $args ) || empty( $args['email'] ) || empty( $args['customer_id'] ) ) { 
  11.  
  12. $output['success'] = false; 
  13.  
  14. if ( empty( $args['email'] ) ) { 
  15. $output['message'] = __( 'Email address is required.', 'easy-digital-downloads' ); 
  16. } else if ( empty( $args['customer_id'] ) ) { 
  17. $output['message'] = __( 'Customer ID is required.', 'easy-digital-downloads' ); 
  18. } else { 
  19. $output['message'] = __( 'Anerrorhas occured. Please try again.', 'easy-digital-downloads' ); 
  20.  
  21. } else if ( ! wp_verify_nonce( $args['_wpnonce'], 'edd-add-customer-email' ) ) { 
  22.  
  23. $output = array( 
  24. 'success' => false,  
  25. 'message' => __( 'Nonce verification failed.', 'easy-digital-downloads' ),  
  26. ); 
  27.  
  28. } else if ( ! is_email( $args['email'] ) ) { 
  29.  
  30. $output = array( 
  31. 'success' => false,  
  32. 'message' => __( 'Invalid email address.', 'easy-digital-downloads' ),  
  33. ); 
  34.  
  35. } else { 
  36.  
  37. $email = sanitize_email( $args['email'] ); 
  38. $customer_id = (int) $args['customer_id']; 
  39. $primary = 'true' === $args['primary'] ? true : false; 
  40. $customer = new EDD_Customer( $customer_id ); 
  41.  
  42. if ( false === $customer->add_email( $email, $primary ) ) { 
  43.  
  44. if ( in_array( $email, $customer->emails ) ) { 
  45.  
  46. $output = array( 
  47. 'success' => false,  
  48. 'message' => __( 'Email already associated with this customer.', 'easy-digital-downloads' ),  
  49. ); 
  50.  
  51. } else { 
  52.  
  53. $output = array( 
  54. 'success' => false,  
  55. 'message' => __( 'Email address is already associated with another customer.', 'easy-digital-downloads' ),  
  56. ); 
  57.  
  58.  
  59. } else { 
  60.  
  61. $redirect = admin_url( 'edit.php?post_type=download&page=edd-customers&view=overview&id=' . $customer_id . '&edd-message=email-added' ); 
  62. $output = array( 
  63. 'success' => true,  
  64. 'message' => __( 'Email successfully added to customer.', 'easy-digital-downloads' ),  
  65. 'redirect' => $redirect,  
  66. ); 
  67.  
  68. $user = wp_get_current_user(); 
  69. $user_login = ! empty( $user->user_login ) ? $user->user_login : 'EDDBot'; 
  70. $customer_note = __( sprintf( 'Email address %s added by %s', $email, $user_login ), 'easy-digital-downloads' ); 
  71. $customer->add_note( $customer_note ); 
  72.  
  73. if ( $primary ) { 
  74. $customer_note = __( sprintf( 'Email address %s set as primary by %s', $email, $user_login ), 'easy-digital-downloads' ); 
  75. $customer->add_note( $customer_note ); 
  76.  
  77.  
  78.  
  79.  
  80. do_action( 'edd_post_add_customer_email', $customer_id, $args ); 
  81.  
  82. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
  83. header( 'Content-Type: application/json' ); 
  84. echo json_encode( $output ); 
  85. wp_die(); 
  86.  
  87. return $output; 
  88.