WC_Discounts::is_coupon_valid()publicWC 3.2.0

Check if a coupon is valid.

Error Codes:

  • 100: Invalid filtered.
  • 101: Invalid removed.
  • 102: Not yours removed.
  • 103: Already applied.
  • 104: Individual use only.
  • 105: Not exists.
  • 106: Usage limit reached.
  • 107: Expired.
  • 108: Minimum spend limit not met.
  • 109: Not applicable.
  • 110: Not valid for sale items.
  • 111: Missing coupon code.
  • 112: Maximum spend limit met.
  • 113: Excluded products.
  • 114: Excluded categories.

Method of the class: WC_Discounts{}

Return

true|false|WP_Error.

Usage

$WC_Discounts = new WC_Discounts();
$WC_Discounts->is_coupon_valid( $coupon );
$coupon(WC_Coupon) (required)
Coupon data.

Changelog

Since 3.2.0 Introduced.

WC_Discounts::is_coupon_valid() code WC 8.6.1

public function is_coupon_valid( $coupon ) {
	try {
		$this->validate_coupon_exists( $coupon );
		$this->validate_coupon_usage_limit( $coupon );
		$this->validate_coupon_user_usage_limit( $coupon );
		$this->validate_coupon_expiry_date( $coupon );
		$this->validate_coupon_minimum_amount( $coupon );
		$this->validate_coupon_maximum_amount( $coupon );
		$this->validate_coupon_product_ids( $coupon );
		$this->validate_coupon_product_categories( $coupon );
		$this->validate_coupon_excluded_items( $coupon );
		$this->validate_coupon_eligible_items( $coupon );

		if ( ! apply_filters( 'woocommerce_coupon_is_valid', true, $coupon, $this ) ) {
			throw new Exception( __( 'Coupon is not valid.', 'woocommerce' ), 100 );
		}
	} catch ( Exception $e ) {
		/**
		 * Filter the coupon error message.
		 *
		 * @param string    $error_message Error message.
		 * @param int       $error_code    Error code.
		 * @param WC_Coupon $coupon        Coupon data.
		 */
		$message = apply_filters( 'woocommerce_coupon_error', is_numeric( $e->getMessage() ) ? $coupon->get_coupon_error( $e->getMessage() ) : $e->getMessage(), $e->getCode(), $coupon );

		return new WP_Error(
			'invalid_coupon',
			$message,
			array(
				'status' => 400,
			)
		);
	}
	return true;
}