LLMS_Student::enroll( int $product_id, string $trigger = 'unspecified' )

Enroll the student into a course or membership


Description Description

See also See also


Top ↑

Parameters Parameters

$product_id

(int) (Required) WP Post ID of the course or membership

$trigger

(string) (Optional) String describing the reason for enrollment

Default value: 'unspecified'


Top ↑

Return Return

(boolean)


Top ↑

Source Source

File: includes/models/model.llms.student.php

	 */
	public function enroll( $product_id, $trigger = 'unspecified' ) {

		/**
		 * Fires before a user is enrolled into a course or membership.
		 *
		 * @param int $user_id WP User ID.
		 * @param int $product_id WP Post ID of the course or membership.
		 */
		do_action( 'before_llms_user_enrollment', $this->get_id(), $product_id );

		// Users can only be enrolled into the following post types.
		if ( ! in_array( get_post_type( $product_id ), llms_get_enrollable_post_types(), true ) ) {
			return false;
		}

		// Check enrollment before enrolling to prevent duplicates.
		if ( llms_is_user_enrolled( $this->get_id(), $product_id ) ) {
			return false;
		}

		// If the student has been previously enrolled, simply update don't run a full enrollment.
		if ( $this->get_enrollment_status( $product_id, false ) ) {
			$insert      = $this->insert_status_postmeta( $product_id, 'enrolled', $trigger );
			$action_type = 'updated';
		} else {
			$insert      = $this->insert_enrollment_postmeta( $product_id, $trigger );
			$action_type = 'created';
		}

		// Add the user postmeta for the enrollment.
		if ( ! empty( $insert ) ) {

			// Update the cache.
			$this->cache_set( sprintf( 'enrollment_status_%d', $product_id ), 'enrolled' );
			$this->cache_delete( sprintf( 'date_enrolled_%d', $product_id ) );
			$this->cache_delete( sprintf( 'date_updated_%d', $product_id ) );

			$post_type = str_replace( 'llms_', '', get_post_type( $product_id ) );

			if ( 'course' === $post_type ) {

				/**
				 * Fires after a user is enrolled in course
				 *
				 * @param int $user_id    WP User ID.
				 * @param int $product_id WP Post ID of the course or membership.
				 */
				do_action( 'llms_user_enrolled_in_course', $this->get_id(), $product_id );

			} elseif ( 'membership' === $post_type ) {

				$this->add_membership_level( $product_id );

				/**
				 * Fires after a user is enrolled in membership
				 *
				 * @param int $user_id    WP User ID.
				 * @param int $product_id WP Post ID of the course or membership.
				 */
				do_action( 'llms_user_added_to_membership_level', $this->get_id(), $product_id );

			}

			/**
			 * Fires after a user's enrollment is created or updated.
			 *
			 * `$post_type` refers to the type of item the user is enrolled in, either 'course' or 'membership'
			 * `$action_type` refers to the type of action taking place, either "created" or "updated".
			 *
			 * @param int $user_id WP User ID.
			 * @param int $product_id WP Post ID of the course or membership.
			 */
			do_action( "llms_user_{$post_type}_enrollment_{$action_type}", $this->get_id(), $product_id );

			return true;

		}

		return false;



Top ↑

Changelog Changelog

Changelog
Version Description
4.4.1 Moved filter llms_user_enrollment_allowed_post_types to function llms_get_enrollable_post_types().
3.37.9 Added filter llms_user_enrollment_allowed_post_types to customize the post types a user can be enrolled into.
3.34.0 Added new actions to differentiate between first-time enrollment and enrollment status updates.
3.17.0 Unknown.
2.2.3 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.