RCP_Discount

Note: This is part of the developer docs and is considered custom code.
Unfortunately, we cannot provide support for custom code at this time as we do not have the additional resources that would be necessary to provide support for custom code.

If you need assistance with this, please reach out to our list of consultants for further assistance:
https://codeable.io/developers/restrict-content-pro/
Table of Contents

Getting a discount
Retrieving discount data
Helper methods

Table of Contents The RCP_Discount class was released in version 3.3. It contains information about a single discount and provider 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:

rcp_get_discounts()

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:

get_id()
get_name()
get_description()
get_amount()
get_unit()
get_code()
get_use_count()
get_max_uses()
get_status()
get_expiration()
get_membership_level_ids()
is_one_time()
is_valid()

get_id()
Returns the ID number of the discount.
get_name()
Returns the name of the discount.
get_description()
Returns the description of the discount.
get_amount()
Returns the discount amount.
get_unit()
Returns the discount unit. This will either be % or flat.
get_code()
Returns the discount code.
get_use_count()
Returns the number of times the discount has been used.
get_max_uses()
Returns the maximum number of times the discount may be used. Returns 0 if there is no limit.
get_status()
Returns the status of the discount code. Will return either active or disabled.
get_expiration()
Returns the date the discount expires, or an empty string if there is no expiration date.
get_membership_level_ids()
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.
is_one_time()
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.

Have more questions?

Submit a request