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

Enroll the student in 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' ) {

		do_action( 'before_llms_user_enrollment', $this->get_id(), $product_id );

		// can only be enrolled in the following post types
		$product_type = get_post_type( $product_id );
		if ( ! in_array( $product_type, array( 'course', 'llms_membership' ) ) ) {
			return false;
		}

		// check enrollment before enrolling
		// this will prevent duplicate enrollments
		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 );
		} else {
			$insert = $this->insert_enrollment_postmeta( $product_id, $trigger );
		}

		// 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 ) );

			// trigger additional actions based off post type
			switch ( get_post_type( $product_id ) ) {

				case 'course':

					do_action( 'llms_user_enrolled_in_course', $this->get_id(), $product_id );

				break;

				case 'llms_membership':

					$this->add_membership_level( $product_id );
					do_action( 'llms_user_added_to_membership_level', $this->get_id(), $product_id );

				break;

			}

			return true;

		}

		return false;

	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.2.3 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

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





Permalink: