Action uncanny-automator-pro

learnpress/user/course-enrolled

Fires when a user successfully enrolls in a course, passing order, course, and user details.

add_action( 'learnpress/user/course-enrolled', $callback, 10, 3 );

Description

Fires after a user successfully enrolls in a LearnPress course. Developers can use this hook to trigger custom actions, log enrollment data, or integrate with other systems. It provides the order ID, course ID, and user ID for contextual use.


Usage

add_action( 'learnpress/user/course-enrolled', 'your_function_name', 10, 3 );

Parameters

$order_id (mixed)
The ID of the order associated with the course enrollment.
$course (mixed)
This parameter contains the ID of the order that resulted in the course enrollment.
$user (mixed)
This parameter contains information about the course that the user has enrolled in.

Examples

<?php

/**
 * Example function hooked to the 'learnpress/user/course-enrolled' action.
 * This function will be executed whenever a user is successfully enrolled in a LearnPress course.
 *
 * @param int $order_id The ID of the order associated with the enrollment.
 * @param int $course_id The ID of the course the user enrolled in.
 * @param int $user_id   The ID of the user who enrolled in the course.
 */
function my_custom_learnpress_enrollment_handler( $order_id, $course_id, $user_id ) {
    // Log the enrollment event for debugging or tracking purposes.
    error_log( sprintf(
        'User ID %d enrolled in Course ID %d via Order ID %d.',
        $user_id,
        $course_id,
        $order_id
    ) );

    // You can perform various actions here, for example:
    // 1. Granting badges or certificates.
    // 2. Sending a custom notification email.
    // 3. Updating user meta.
    // 4. Triggering other integrations.

    // Example: Update user meta to mark them as enrolled in a specific course.
    // Note: In a real-world scenario, you'd likely want to check if this is a new enrollment
    // or if the user was already enrolled to avoid redundant updates.
    // For simplicity, this example directly updates the meta.
    $course_meta_key = 'learnpress_enrolled_course_' . $course_id;
    update_user_meta( $user_id, $course_meta_key, true );

    // Example: If you wanted to send a welcome email for the course.
    // $user_info = get_userdata( $user_id );
    // $course_info = learn_press_get_course( $course_id );
    // if ( $user_info && $course_info ) {
    //     $subject = sprintf( 'Welcome to %s!', $course_info->get_title() );
    //     $message = sprintf( 'Hello %s, we are excited to have you enrolled in the %s course. Start learning now!', $user_info->display_name, $course_info->get_title() );
    //     wp_mail( $user_info->user_email, $subject, $message );
    // }
}

// Add the action hook with the custom handler function.
// The third parameter '3' indicates that the function accepts 3 arguments.
add_action( 'learnpress/user/course-enrolled', 'my_custom_learnpress_enrollment_handler', 10, 3 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

uncanny-automator-pro/src/integrations/learnpress/actions/lp-enrlcourse-a.php:149

Automator()->complete_action( $user_id, $action_data, $recipe_id, esc_html_x( "Error: Can't Enroll course.", 'LearnPress', 'uncanny-automator-pro' ) );

				return;
			}

			Automator()->complete_action( $user_id, $action_data, $recipe_id );

			do_action( 'learnpress/user/course-enrolled', $order_id, $course->get_id(), $user->get_id() ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores

		} else {
			$args['do-nothing']                  = true;
			$action_data['do-nothing']           = true;
			$action_data['complete_with_errors'] = true;
			Automator()->complete_action( $user_id, $action_data, $recipe_id, esc_html_x( 'Course not found.', 'LearnPress', 'uncanny-automator-pro' ) );

Scroll to Top