WC_Discounts::is_coupon_valid( WC_Coupon $coupon )
Check if a coupon is valid.
Description Description
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.
Parameters Parameters
- $coupon
-
(Required) Coupon data.
Return Return
(bool|WP_Error)
Source Source
File: includes/class-wc-discounts.php
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;
}
Changelog Changelog
| Version | Description |
|---|---|
| 3.2.0 | Introduced. |