Home / Widgets / Tutor LMS – Woocommerce Create Account After Payment
Duplicate Snippet

Embed Snippet on Your Site

Tutor LMS – Woocommerce Create Account After Payment

Gửi email tài khoản sau khi đã "completed" đơn hàng trên Woocommerce (Cartflows) -> kích hoạt khóa học trên Tutor LMS tương ứng

Code Preview
php
<?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();
    
    // Find user by email
    $user = get_user_by('email', $customer_email);
    if ($user) {
        // Existing user found, update 'customer' role
        if (!in_array('customer', $user->roles)) {
            $user->add_role('customer');
        }
        // Update order to link to customer profile
        $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();
        // Enroll user in courses based on the order
        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 {
        // New user, create account and enroll in courses
        $first_name = $order->get_billing_first_name();
        $last_name = $order->get_billing_last_name();
        // Generate a random password for the user
        $password = wp_generate_password();
        // Create a new user
        $user_id = wp_create_user($customer_email, $password, $customer_email);
        if (!is_wp_error($user_id)) {
            // Update user information and assign 'customer' role
            wp_update_user([
                'ID'         => $user_id,
                'first_name' => $first_name,
                'last_name'  => $last_name,
                'role'       => 'customer'
            ]);
            // Send account creation email to the user
            WC()->mailer()->emails['WC_Email_Customer_New_Account']->trigger($user_id, $password, true);
            // Set the customer ID on the order
            update_post_meta($order_id, '_customer_user', $user_id);
            // Enroll user in courses based on the order
            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

Add a Comment