llms_setup_pending_order( array $data = array() )

Setup a pending order which can be passed to an LLMS_Payment_Gateway for processing.


Description Description


Parameters Parameters

$data

(array) (Optional) Data used to create a pending order.

  • 'plan_id'
    (int) (Required) LLMS_Access_Plan ID.
  • 'customer'
    (array) (Required). Array of customer information formatted to be passed to LLMS_Person_Handler::update() or llms_register_user()
  • 'agree_to_terms'
    (string) (Required if llms_are_terms_and_conditions_required() are required) If terms & conditions are required this should be "yes" for agreement.
  • 'payment_gateway'
    (string) (Optional) ID of a registered LLMS_Payment_Gateway which will be used to process the order.
  • 'coupon_code'
    (string) (Optional) Coupon code to be applied to the order.

Default value: array()


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/functions/llms.functions.order.php

function llms_setup_pending_order( $data = array() ) {

	/**
	 * @filter llms_before_setup_pending_order
	 */
	$data = apply_filters( 'llms_before_setup_pending_order', $data );

	// Request keys that can be submitted with or without the `llms_` prefix.
	$keys = array(
		'llms_agree_to_terms',
		'llms_coupon_code',
		'llms_plan_id',
	);
	foreach ( $keys as $key ) {
		if ( isset( $data[ $key ] ) ) {
			$data[ str_replace( 'llms_', '', $key ) ] = $data[ $key ];
		}
	}

	$err = new WP_Error();

	// check t & c if configured
	if ( llms_are_terms_and_conditions_required() ) {
		if ( ! isset( $data['agree_to_terms'] ) || ! llms_parse_bool( $data['agree_to_terms'] ) ) {
			$err->add( 'terms-violation', sprintf( __( 'You must agree to the %s to continue.', 'lifterlms' ), get_the_title( get_option( 'lifterlms_terms_page_id' ) ) ) );
			return $err;
		}
	}

	// we must have a plan_id to proceed
	if ( empty( $data['plan_id'] ) ) {
		$err->add( 'missing-plan-id', __( 'Missing an Access Plan ID.', 'lifterlms' ) );
		return $err;
	}

	// validate the plan is a real plan
	$plan = llms_get_post( absint( $data['plan_id'] ) );
	if ( ! $plan || 'llms_access_plan' !== $plan->get( 'type' ) ) {
		$err->add( 'invalid-plan-id', __( 'Invalid Access Plan ID.', 'lifterlms' ) );
		return $err;
	}

	// used later
	$coupon_id = null;
	$coupon = false;

	// if a coupon is being used, validate it
	if ( ! empty( $data['coupon_code'] ) ) {

		$coupon_id = llms_find_coupon( $data['coupon_code'] );

		// coupon couldn't be found
		if ( ! $coupon_id ) {
			$err->add( 'coupon-not-found', sprintf( __( 'Coupon code "%s" not found.', 'lifterlms' ), $data['coupon_code'] ) );
			return $err;
		}

		// coupon is real, make sure it's valid for the current plan
		$coupon = llms_get_post( $coupon_id );
		$valid = $coupon->is_valid( $data['plan_id'] );

		// if the coupon has a validation error, return an error message
		if ( is_wp_error( $valid ) ) {
			$err->add( 'invalid-coupon', $valid->get_error_message() );
			return $err;
		}
	}

	// if payment is required, verify we have a gateway
	if ( $plan->requires_payment( $coupon_id ) && empty( $data['payment_gateway'] ) ) {
		$err->add( 'missing-gateway-id', __( 'No payment method selected.', 'lifterlms' ) );
		return $err;
	}

	$gateway_id = empty( $data['payment_gateway'] ) ? 'manual' : $data['payment_gateway'];
	$gateway_error = llms_can_gateway_be_used_for_plan( $gateway_id, $plan );
	if ( is_wp_error( $gateway_error ) ) {
		return $gateway_error;
	}

	if ( empty( $data['customer'] ) ) {
		$err->add( 'missing-customer', __( 'Missing customer information.', 'lifterlms' ) );
		return $err;
	}

	// update the customer
	if ( ! empty( $data['customer']['user_id'] ) ) {
		$person_id = LLMS_Person_Handler::update( $data['customer'], 'checkout' );
	} else {
		$person_id = llms_register_user( $data['customer'], 'checkout', true );
	}

	// validation or registration issues
	if ( is_wp_error( $person_id ) ) {
		return $person_id;
	}

	// this will likely never actually happen unless there's something very strange afoot
	if ( ! is_numeric( $person_id ) ) {

		$err->add( 'account-creation', __( 'An unknown error occurred when attempting to create an account, please try again.', 'lifterlms' ) );
		return $err;

	}

	// ensure the new user isn't enrolled in the product being purchased
	if ( llms_is_user_enrolled( $person_id, $plan->get( 'product_id' ) ) ) {

		$product = $plan->get_product();
		$err->add( 'already-enrolled', sprintf(
			__( 'You already have access to this %2$s! Visit your dashboard <a href="%s">here.</a>', 'lifterlms' ),
			llms_get_page_url( 'myaccount' ), $product->get_post_type_label()
		) );
		return $err;
	}

	$person = llms_get_student( $person_id );
	$gateway = LLMS()->payment_gateways()->get_gateway_by_id( $gateway_id );

	/**
	 * Filter the return of pending order setup data.
	 *
	 * @since 3.30.1
	 * @version 3.30.1
	 *
	 * @param $setup {
		 *     Data used to create the pending order.
		 *
		 *     @type LLMS_Student $person Student object.
		 *     @type LLMS_Access_Plan $plan Access plan object.
		 *     @type LLMS_Payment_Gateway $gateway Instance of the selected gateway.
		 *     @type LLMS_Coupon|false $coupon Coupon object or false if none used.
	 * }
	 * @param array $data Array of input data from a checkout form.
	 */
	return apply_filters( 'llms_after_setup_pending_order', compact( 'person', 'plan', 'gateway', 'coupon' ), $data );

}

Top ↑

Changelog Changelog

Changelog
Version Description
3.29.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

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





Permalink: