llms_update_300_create_access_plans()

Creates access plans for each course & membership


Description Description

Creates up to 3 plans per course and up to two plans per membership.

Migrates price & subscription data to a single & recurring plan where applicable.

If course is restricted to a membership a free members only plan will be created in addition to paid open recurring & single plans.

If course is restricted to a membership and no price is found only one free members only plan will be created.


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/functions/updates/llms-functions-updates-300.php

function llms_update_300_create_access_plans() {

	$courses = new WP_Query(
		array(
			'post_type'      => array( 'course', 'llms_membership' ),
			'posts_per_page' => -1,
			'status'         => 'any',
		)
	);

	if ( $courses->have_posts() ) {
		foreach ( $courses->posts as $post ) {

			$meta = get_post_meta( $post->ID );

			$is_free       = ( ! $meta['_price'][0] || floatval( 0 ) === floatval( $meta['_price'][0] ) );
			$has_recurring = ( 1 == $meta['_llms_recurring_enabled'][0] );
			if ( 'course' === $post->post_type ) {
				$members_only = ( 'on' === $meta['_llms_is_restricted'][0] && $meta['_llms_restricted_levels'][0] );
			} else {
				$members_only = false;
			}

			// Base plan for single & recurring.
			$base_plan = array(

				'access_expiration'         => 'lifetime',
				'availability'              => 'open',
				'availability_restrictions' => array(),
				'content'                   => '',
				'enroll_text'               => ( 'course' === $post->post_type ) ? __( 'Enroll', 'lifterlms' ) : __( 'Join', 'lifterlms' ),
				'featured'                  => 'no',
				'frequency'                 => 0,
				'is_free'                   => 'no',
				'product_id'                => $post->ID,
				'sku'                       => $meta['_sku'][0],
				'trial_offer'               => 'no',

			);

			$single = array_merge(
				array(
					'price' => $meta['_price'][0],
				),
				$base_plan
			);

			$recurring = array_merge(
				array(
					'price' => $meta['_llms_subscription_price'][0],
				),
				$base_plan
			);

			/**
			 * Determine what kinds of plans to create
			 */

			// Free and members only, only available to members.
			if ( $is_free && $members_only ) {

				$free_members_only = true;
				$single_paid_open  = false;
				$single_free_open  = false;
				$recurring_paid    = false;

			} elseif ( ! $is_free && $members_only ) {

				$free_members_only = true;
				$single_paid_open  = true;
				$single_free_open  = false;
				$recurring_paid    = $has_recurring;

			} else {
				// No restrictions, normal settings apply.

				$free_members_only = false;
				$single_paid_open  = ! $is_free ? true : false;
				$single_free_open  = $is_free ? true : false;
				$recurring_paid    = $has_recurring;

			}

			$order = 1;

			/**
			 * CREATE THE PLANS
			 */
			if ( $free_members_only ) {

				$plan                              = $single;
				$plan['menu_order']                = $order;
				$plan['is_free']                   = 'yes';
				$plan['sku']                       = ! empty( $plan['sku'] ) ? $plan['sku'] . '-membersonly' : '';
				$plan['availability']              = 'members';
				$plan['availability_restrictions'] = unserialize( $meta['_llms_restricted_levels'][0] );

				$obj = new LLMS_Access_Plan( 'new', __( 'Members Only', 'lifterlms' ) );
				foreach ( $plan as $key => $val ) {
					$obj->set( $key, $val );
				}

				unset( $plan );
				$order++;

			}

			if ( $single_paid_open ) {

				$plan               = $single;
				$plan['menu_order'] = $order;
				$plan['sku']        = ! empty( $plan['sku'] ) ? $plan['sku'] . '-onetime' : '';
				$plan['on_sale']    = ! empty( $meta['_sale_price'][0] ) ? 'yes' : 'no';

				if ( 'yes' === $plan['on_sale'] ) {

					$plan['sale_end']   = ! empty( $meta['_sale_price_dates_to'][0] ) ? date( 'm/d/Y', strtotime( $meta['_sale_price_dates_to'][0] ) ) : '';
					$plan['sale_start'] = ! empty( $meta['_sale_price_dates_from'][0] ) ? date( 'm/d/Y', strtotime( $meta['_sale_price_dates_from'][0] ) ) : '';
					$plan['sale_price'] = $meta['_sale_price'][0];

				}

				$obj = new LLMS_Access_Plan( 'new', __( 'One-Time Payment', 'lifterlms' ) );
				foreach ( $plan as $key => $val ) {
					$obj->set( $key, $val );
				}

				unset( $plan );
				$order++;

			}

			if ( $single_free_open ) {

				$plan               = $single;
				$plan['menu_order'] = $order;
				$plan['is_free']    = 'yes';
				$plan['sku']        = ! empty( $plan['sku'] ) ? $plan['sku'] . '-free' : '';

				$obj = new LLMS_Access_Plan( 'new', __( 'Free', 'lifterlms' ) );
				foreach ( $plan as $key => $val ) {
					$obj->set( $key, $val );
				}

				unset( $plan );
				$order++;

			}

			if ( $recurring_paid ) {

				$plan               = $recurring;
				$plan['menu_order'] = $order;
				$plan['sku']        = ! empty( $plan['sku'] ) ? $plan['sku'] . '-subscription' : '';

				if ( isset( $meta['_llms_subscription_first_payment'][0] ) && $meta['_llms_subscription_first_payment'][0] != $meta['_llms_subscription_price'][0] ) {
					$plan['trial_offer']  = 'yes';
					$plan['trial_length'] = $meta['_llms_billing_freq'][0];
					$plan['trial_period'] = $meta['_llms_billing_period'][0];
					$plan['trial_price']  = $meta['_llms_subscription_first_payment'][0];
				}

				$plan['frequency'] = $meta['_llms_billing_freq'][0];
				$plan['length']    = $meta['_llms_billing_cycle'][0];
				$plan['period']    = $meta['_llms_billing_period'][0];

				$obj = new LLMS_Access_Plan( 'new', __( 'Subscription', 'lifterlms' ) );
				foreach ( $plan as $key => $val ) {
					$obj->set( $key, $val );
				}

				unset( $plan );
				$order++;

			}

			$keys = array(
				'_regular_price',
				'_price',
				'_sale_price',
				'_sale_price_dates_from',
				'_sale_price_dates_to',
				'_on_sale',
				'_llms_recurring_enabled',
				'_llms_subscription_price',
				'_llms_subscription_first_payment',
				'_llms_billing_period',
				'_llms_billing_freq',
				'_llms_billing_cycle',
				'_llms_subscriptions',
				'_sku',
				'_is_custom_single_price',
				'_custom_single_price_html',
				'_llms_is_restricted',
				'_llms_restricted_levels',

				'_llms_expiration_interval',
				'_llms_expiration_period',
			);

			foreach ( $keys as $key ) {
				delete_post_meta( $post->ID, $key );
			}
		}
	}

}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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