LLMS_Access_Plan::get_price_with_coupon( string $key, int $coupon_id, array $price_args = array(), string $format = 'html' )

Apply a coupon to a price


Description Description


Parameters Parameters

$key

(string) (Required) price to retrieve, "price", "sale_price", or "trial_price"

$coupon_id

(int) (Required) LifterLMS Coupon Post ID

$price_args

(array) (Optional) arguments to be passed to llms_price()

Default value: array()

$format

(string) (Optional) return format as passed to llms_price()

Default value: 'html'


Top ↑

Return Return

(mixed)


Top ↑

Source Source

File: includes/models/model.llms.access.plan.php

	public function get_price_with_coupon( $key, $coupon_id, $price_args = array(), $format = 'html' ) {

		// allow id or instance to be passed for $coupon_id
		if ( $coupon_id instanceof LLMS_Coupon ) {
			$coupon = $coupon_id;
		} else {
			$coupon = new LLMS_Coupon( $coupon_id );
		}

		$price = $this->get( $key );

		// ensure the coupon *can* be applied to this plan
		if ( ! $coupon->is_valid( $this ) ) {
			return $price;
		}

		$discount_type = $coupon->get( 'discount_type' );

		// price and sale price are calculated of coupon amount
		if ( 'price' === $key || 'sale_price' === $key ) {

			$coupon_amount = $coupon->get( 'coupon_amount' );

		} elseif ( 'trial_price' === $key && $coupon->has_trial_discount() && $this->has_trial() ) {

			$coupon_amount = $coupon->get( 'trial_amount' );

		} else {

			$coupon_amount = 0;

		}

		if ( $coupon_amount ) {

			// simple subtraction
			if ( 'dollar' === $discount_type ) {
				$price = $price - $coupon_amount;
			} // End if().
			elseif ( 'percent' === $discount_type ) {
				$price = $price - ( $price * ( $coupon_amount / 100 ) );
			}
		}

		// if price is less than 0 return the pricing text
		if ( $price <= 0 ) {

			$price = $this->get_free_pricing_text( $format );

		} else {

			if ( 'html' == $format || 'raw' === $format ) {
				$price = llms_price( $price, $price_args );
				if ( 'raw' === $format ) {
					$price = strip_tags( $price );
				}
			} elseif ( 'float' === $format ) {
				$price = floatval( number_format( $price, get_lifterlms_decimals(), '.', '' ) );
			} else {
				$price = apply_filters( 'llms_get_' . $this->model_post_type . '_' . $key . '_' . $format . '_with_coupon', $price, $key, $price_args, $format, $this );
			}
		}

		return apply_filters( 'llms_get_' . $this->model_post_type . '_' . $key . '_price_with_coupon', $price, $key, $price_args, $format, $this );

	}

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.





Permalink: