|  | 
 | <?php
  | 
 | add_action('woocommerce_order_status_completed', 'create_user_and_notify_on_order_completion');
  | 
 | 
  | 
 | function create_user_and_notify_on_order_completion($order_id) {
  | 
 |     $order = wc_get_order($order_id);
  | 
 |     $customer_email = $order->get_billing_email();
  | 
 |     
  | 
 |     
  | 
 |     $user = get_user_by('email', $customer_email);
  | 
 | 
  | 
 |     if ($user) {
  | 
 |         
  | 
 |         if (!in_array('customer', $user->roles)) {
  | 
 |             $user->add_role('customer');
  | 
 |         }
  | 
 | 
  | 
 |         
  | 
 |         $order->set_customer_id($user->ID);
  | 
 |         $order->set_billing_email($customer_email);
  | 
 |         $order->set_billing_first_name($user->first_name);
  | 
 |         $order->set_billing_last_name($user->last_name);
  | 
 |         $order->save();
  | 
 | 
  | 
 |         
  | 
 |         foreach ($order->get_items() as $item) {
  | 
 |             $product_id = $item->get_product_id();
  | 
 |             $if_has_course = tutor_utils()->product_belongs_with_course($product_id);
  | 
 |             if ($if_has_course) {
  | 
 |                 $course_id = $if_has_course->post_id;
  | 
 |                 tutor_utils()->do_enroll($course_id, $order_id, $user->ID);
  | 
 |             }
  | 
 |         }
  | 
 |     } else {
  | 
 |         
  | 
 |         $first_name = $order->get_billing_first_name();
  | 
 |         $last_name = $order->get_billing_last_name();
  | 
 | 
  | 
 |         
  | 
 |         $password = wp_generate_password();
  | 
 | 
  | 
 |         
  | 
 |         $user_id = wp_create_user($customer_email, $password, $customer_email);
  | 
 | 
  | 
 |         if (!is_wp_error($user_id)) {
  | 
 |             
  | 
 |             wp_update_user([
  | 
 |                 'ID'         => $user_id,
  | 
 |                 'first_name' => $first_name,
  | 
 |                 'last_name'  => $last_name,
  | 
 |                 'role'       => 'customer'
  | 
 |             ]);
  | 
 | 
  | 
 |             
  | 
 |             WC()->mailer()->emails['WC_Email_Customer_New_Account']->trigger($user_id, $password, true);
  | 
 | 
  | 
 |             
  | 
 |             update_post_meta($order_id, '_customer_user', $user_id);
  | 
 | 
  | 
 |             
  | 
 |             foreach ($order->get_items() as $item) {
  | 
 |                 $product_id = $item->get_product_id();
  | 
 |                 $if_has_course = tutor_utils()->product_belongs_with_course($product_id);
  | 
 |                 if ($if_has_course) {
  | 
 |                     $course_id = $if_has_course->post_id;
  | 
 |                     tutor_utils()->do_enroll($course_id, $order_id, $user_id);
  | 
 |                 }
  | 
 |             }
  | 
 |         } else {
  | 
 |             error_log('Failed to create user for order ' . $order_id . ': ' . $user_id->get_error_message());
  | 
 |         }
  | 
 |     }
  | 
 | }
  | 
 | 
  | 
 |  | 
 |  | 
Comments