The RCP_Discount class was released in version 3.3. It contains information about a single discount and providers helper methods for interacting with the discount.
Getting a discount
The main way to retrieve a discount object is with the rcp_get_discount() function. This requires that you know the discount ID number. Here's an example:
$discount = rcp_get_discount( 123 );
You can also use rcp_get_discount_by() to retrieve a single discount by one of its values, such as the discount name or code. Example:
$discount = rcp_get_discount_by( 'code', 'beta_tester' );
Alternatively, some of our other helper functions will return RCP_Discount objects automatically, including:
Do not create a new instance of the RCP_Discount class manually.
Retrieving discount data
Once you have an RCP_Discount object, you can use it to retrieve all the information about the discount. Here's a list of methods:
Returns the ID number of the discount.
Returns the name of the discount.
Returns the description of the discount.
Returns the discount amount.
Returns the discount unit. This will either be
Returns the discount code.
Returns the number of times the discount has been used.
Returns the maximum number of times the discount may be used. Returns
0 if there is no limit.
Returns the status of the discount code. Will return either
Returns the date the discount expires, or an empty string if there is no expiration date.
Returns an array of membership level IDs that the discount code can be used on. Returns an empty array of the discount can be used on any membership level.
Returns true if this is a one-time discount, or false if it's a recurring discount.
is_valid( $membership_level_id = 0 )
Determines whether or not a discount code is valid for use, and may be used with the supplied membership level ID. This method returns true if all the following conditions are met:
- The discount status is "active".
- The expiration date is either not configured or is in the future.
- The maximum number of uses has not been exceeded.
- The provided membership level ID is in the allowed list of levels (or the discount is set up to allow any level).
Other helper methods
- increment_use_count() - Increase the use count by one.
- decrement_use_count() - Decrease the use count by one.
- is_expired() - Returns true if the discount has an expiration date set and that date is in the past.
- is_maxed_out() - Returns true if the discount has a maximum number of uses set and that limit has been reached.
- has_membership_level_ids() - Returns true if the discount is locked to one or more membership levels.
- store_for_user( $user_id ) - Stores this discount code in a user's history ("rcp_user_discounts" meta).
- remove_from_user( $user_id ) - Removes this discount code from a user's history.