Advertisement
fahimmurshed

Create user if one does not exist

Oct 30th, 2021
1,729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.72 KB | None | 0 0
  1. // Create user if one does not exist
  2. function tn_checkout_create_acct( $post_data ) {
  3.     $user = get_user_by( 'email', $post_data['billing_email'] );
  4.     if ( $user ) {
  5.         $post_data['createaccount'] = 0;
  6.     } else {
  7.         $post_data['createaccount'] = 1;
  8.     }
  9.     return $post_data;
  10. }
  11.  
  12. add_filter('woocommerce_checkout_posted_data', 'tn_checkout_create_acct');
  13.  
  14. // Attach order to existing account if user not logged in
  15. function tn_checkout_set_customer_id( $current_user_id ) {
  16.     if ( !$current_user_id ) {
  17.         $user = get_user_by('email', $_POST['billing_email']);
  18.         if ( $user ) {
  19.             $current_user_id = $user->ID;
  20.         }
  21.     }
  22.     return $current_user_id;
  23. }
  24.  
  25. add_filter('woocommerce_checkout_customer_id', 'tn_checkout_set_customer_id');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement