Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. function wc_register_guests( $order_id ) {
  2. // get all the order data
  3. $order = new WC_Order($order_id);
  4.  
  5. //get the user email from the order
  6. $order_email = $order->billing_email;
  7.  
  8. // check if there are any users with the billing email as user or email
  9. $email = email_exists( $order_email );
  10. $user = username_exists( $order_email );
  11.  
  12. // if the UID is null, then it's a guest checkout
  13. if( $user == false && $email == false ){
  14.  
  15. // random password with 12 chars
  16. $random_password = wp_generate_password();
  17.  
  18. // create new user with email as username & newly created pw
  19. $user_id = wp_create_user( $order_email, $random_password, $order_email );
  20.  
  21. //WC guest customer identification
  22. update_user_meta( $user_id, 'guest', 'yes' );
  23.  
  24. //user's billing data
  25. update_user_meta( $user_id, 'billing_address_1', $order->billing_address_1 );
  26. update_user_meta( $user_id, 'billing_address_2', $order->billing_address_2 );
  27. update_user_meta( $user_id, 'billing_city', $order->billing_city );
  28. update_user_meta( $user_id, 'billing_company', $order->billing_company );
  29. update_user_meta( $user_id, 'billing_country', $order->billing_country );
  30. update_user_meta( $user_id, 'billing_email', $order->billing_email );
  31. update_user_meta( $user_id, 'billing_first_name', $order->billing_first_name );
  32. update_user_meta( $user_id, 'billing_last_name', $order->billing_last_name );
  33. update_user_meta( $user_id, 'billing_phone', $order->billing_phone );
  34. update_user_meta( $user_id, 'billing_postcode', $order->billing_postcode );
  35. update_user_meta( $user_id, 'billing_state', $order->billing_state );
  36.  
  37. // user's shipping data
  38. update_user_meta( $user_id, 'shipping_address_1', $order->shipping_address_1 );
  39. update_user_meta( $user_id, 'shipping_address_2', $order->shipping_address_2 );
  40. update_user_meta( $user_id, 'shipping_city', $order->shipping_city );
  41. update_user_meta( $user_id, 'shipping_company', $order->shipping_company );
  42. update_user_meta( $user_id, 'shipping_country', $order->shipping_country );
  43. update_user_meta( $user_id, 'shipping_first_name', $order->shipping_first_name );
  44. update_user_meta( $user_id, 'shipping_last_name', $order->shipping_last_name );
  45. update_user_meta( $user_id, 'shipping_method', $order->shipping_method );
  46. update_user_meta( $user_id, 'shipping_postcode', $order->shipping_postcode );
  47. update_user_meta( $user_id, 'shipping_state', $order->shipping_state );
  48.  
  49. // link past orders to this newly created customer
  50. wc_update_new_customer_past_orders( $user_id );
  51. }
  52.  
  53. }
  54.  
  55. //add this newly created function to the thank you page
  56. add_action( 'woocommerce_thankyou', 'wc_register_guests', 10, 1 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement