LLMS_Voucher::use_voucher( string $code, int $user_id )

Attempt to redeem a voucher for a user with a code


Description Description


Parameters Parameters

$code

(string) (Required) voucher code of the voucher being redeemed

$user_id

(int) (Required) user id of the redeeming user


Top ↑

Return Return

(bool|WP_Error) true on success or WP_Error on failure


Top ↑

Source Source

File: includes/class.llms.voucher.php

	public function use_voucher( $code, $user_id ) {

		$code = sanitize_text_field( $code );

		$voucher = $this->check_voucher( $code );

		if ( ! is_wp_error( $voucher ) ) {

			$this->id = $voucher->voucher_id;

			// ensure the user hasn't already redeemed this voucher
			if ( $this->get_redemptions_for_code_by_user( $voucher->id, $user_id ) ) {

				return new WP_Error( 'error', __( 'You have already redeemed this voucher.', 'lifterlms' ) );

			}

			// get products linked to the voucher
			$products = $this->get_products();

			if ( ! empty( $products ) ) {

				// loop through all of them and attempt enrollment
				foreach ( $products as $product ) {

					llms_enroll_student( $user_id, $product, 'voucher' );

				}

				do_action( 'llms_voucher_used', $voucher->id, $user_id, $voucher->code );

				// use voucher code
				$data = array(
					'user_id' => $user_id,
					'code_id' => $voucher->id,
				);
				$this->save_redeemed_code( $data );

				return true;

			}
		} else {

			return $voucher ;

		}// End if().

	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

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





Permalink: