Creating user in Moodle when making a purchase in WooCommerce

In the online education field, seamless integration between platforms is crucial for providing a smooth user experience. If you use Moodle to deliver online courses and also manage an online store on WooCommerce, automating the creation of users in Moodle when a purchase is made on WooCommerce can be a significant advantage. In this guide, you will learn how to generate API credentials in Moodle, understand the structure of a Moodle token, and configure WooCommerce to automatically create users in Moodle after a purchase.

Generating and obtaining API credentials in Moodle

 

  1. Log in to your Moodle instance: Access your Moodle platform as an administrator.
  2. Access web services configuration: Navigate to “Site Administration” > “Development” > “Web Services”.
  3. Create a new web service: Click on “Add service”. Assign a descriptive name and save the changes.
  4. Generate an access Token: Within the newly created web service, click on “Add a new token”. Select the required capabilities for the web service and save the generated token.

Example of a Moodle Token structure

A Moodle token has a structure similar to this:

18a42b7e564a0d9d1e8c64a02a28545d

This token is unique for each user and provides access to Moodle APIs.

 

Creating User in Moodle When Making a Purchase in WooCommerce

add_action('woocommerce_payment_complete', 'create_user_in_moodle_from_woocommerce');

function create_user_in_moodle_from_woocommerce($order_id) {
    $order = wc_get_order($order_id);

    $first_name = $order->get_billing_first_name();
    $last_name = $order->get_billing_last_name();
    $email = $order->get_billing_email();
    $phone = $order->get_billing_phone();
    $dni = $order->get_meta('dni');

    // Customize the user role as needed
    $role = 'student';

    $moodle_api_url = 'https://yoursite.com/moodle/webservice/rest/server.php';
    $token = 'your_access_token';

    $moodle_user = array(
        'username' => $email,
        'password' => wp_generate_password(12, false),
        'firstname' => $first_name,
        'lastname' => $last_name,
        'email' => $email,
        'phone1' => $phone,
        'customfields' => array(
            array(
                'type' => 'DNI',
                'value' => $dni
            )
        ),
        'role' => $role
    );

    $response = wp_remote_post($moodle_api_url, array(
        'body' => array(
            'wstoken' => $token,
            'wsfunction' => 'core_user_create_users',
            'moodlewsrestformat' => 'json',
            'users' => array($moodle_user)
        )
    ));

    if (is_wp_error($response)) {
        // Handle the error
    } else {
        $response_body = wp_remote_retrieve_body($response);
        $response_data = json_decode($response_body, true);

        if (!empty($response_data['exception'])) {
            // Handle the exception
        } else {
            // User successfully created in Moodle
        }
    }
}

 

 

Integrating Moodle and WooCommerce to automatically create users in Moodle after a purchase on WooCommerce can significantly improve the user experience and save administrative time. With the API credentials generated in Moodle and the proper configuration in WooCommerce, along with the provided PHP code, you will be able to effectively implement this integration in your e-learning and e-commerce environment.

You are now ready to start integrating Moodle and WooCommerce effectively!

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Back to Top
0
Would love your thoughts, please comment.x
()
x