edd_add_past_purchases_to_new_user

Looks up purchases by email that match the registering user.

Description

edd_add_past_purchases_to_new_user( (int) $user_id ); 

This is for users that purchased as a guest and then came back and created an account.

Parameters (1)

0. $user_id (int)
- the new user's ID

Usage

  1. if ( !function_exists( 'edd_add_past_purchases_to_new_user' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'easy-digital-downloads/includes/user-functions.php'; 
  3.  
  4. // - the new user's ID 
  5. $user_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = edd_add_past_purchases_to_new_user($user_id); 
  9.  

Defined (1)

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

/includes/user-functions.php  
  1. function edd_add_past_purchases_to_new_user( $user_id ) { 
  2.  
  3. $email = get_the_author_meta( 'user_email', $user_id ); 
  4.  
  5. $payments = edd_get_payments( array( 's' => $email ) ); 
  6.  
  7. if( $payments ) { 
  8.  
  9. // Set a flag to force the account to be verified before purchase history can be accessed 
  10. edd_set_user_to_pending( $user_id ); 
  11.  
  12.  
  13. foreach( $payments as $payment ) { 
  14. if( intval( edd_get_payment_user_id( $payment->ID ) ) > 0 ) { 
  15. continue; // This payment already associated with an account 
  16.  
  17. $meta = edd_get_payment_meta( $payment->ID ); 
  18. $meta['user_info'] = maybe_unserialize( $meta['user_info'] ); 
  19. $meta['user_info']['id'] = $user_id; 
  20. $meta['user_info'] = $meta['user_info']; 
  21.  
  22. // Store the updated user ID in the payment meta 
  23. edd_update_payment_meta( $payment->ID, '_edd_payment_meta', $meta ); 
  24. edd_update_payment_meta( $payment->ID, '_edd_payment_user_id', $user_id ); 
  25.