Restrict Content Pro
RCP_Discounts
Coming soon: Restrict Content Pro is becoming Kadence Memberships. You may see both names used while we update the product, documentation, and customer experience.
Note: This content is provided as a resource within our developer documentation. Because custom code requires highly specific implementation, we encourage you to work with a qualified developer if you need guidance. You can find our recommended consultants here: https://codeable.io/developers/restrict-content-pro/
RCP_Discounts
This class was deprecated in Restrict Content Pro 3.3. For new code, use rcp_get_discounts().
RCP_Discounts class allows you to interact with the discounts database table.
get_discounts( $args = array() )
Retrieve discounts from the database. You can filter by discount status like so:
$discounts_db = new RCP_Discounts();
$discounts = $discounts_db->get_discounts( array( 'status' => 'active' ) );
get_discount( $discount_id = 0 )
Retrieve a specific discount from the database by its ID number. This returns the object from the database like so:
stdClass Object
(
[id] => 7
[name] => 10% off
[description] =>
[amount] => 10
[unit] => %
[code] => 10percent
[use_count] => 7
[max_uses] => 0
[status] => active
[expiration] =>
[subscription_id] => 0
)
get_by( $field = ‘code’, $value = ” )
Retrieve a specific discount code from the database by a field. You can use this to get a discount by its code rather than ID number. Example:
$discounts_db = new RCP_Discounts();
$discount = $discounts_db->get_by( 'code', 'half_off' );
insert( $args = array() )
Used for inserting a new discount into the database. Available arguments are:
name(string) – Name of the discount code.description(string) – Discount description.amount(float) – Amount the discount is for.status(string) – Eitheractiveorinactive. Default isinactive.unit(string) – Eitherflator%. Default is%.code(string) – Discount code.expiration(string) – Date the discount expires inY-m-dformat. Default is empty string (no expiration).max_uses(int) – Maximum number of uses. Use0for unlimited.use_count(int) – Number of times the code has been used. Default is0.subscription_id(int) – Associated subscription ID. Default is0(no association).
update( $discount_id = 0, $args = array() )
Update an existing discount code in the database. Arguments are the same as insert().
Additional helper methods:
get_status( $discount_id = 0 )— Status of a discount.get_amount( $discount_id = 0 )— Discounted amount.get_uses( $discount_id = 0 )— Times the discount has been used.get_max_uses( $discount_id = 0 )— Maximum uses.get_subscription_id( $discount_id = 0 )— Associated membership level.has_subscription_id( $discount_id = 0 )— Whether a membership level is attached.increase_uses( $discount_id = 0 )— Increase the use count by one.get_expiration( $discount_id = 0 )— Expiration date.get_type( $discount_id = 0 )—flator%.delete( $discount_id = 0 )— Delete the discount.is_maxed_out( $discount_id = 0 )— Whether max uses are reached.is_expired( $discount_id = 0 )— Whether the code is expired.add_to_user( $user_id = 0, $discount_code = '' )— Add a code to a user’s history.user_has_used( $user_id = 0, $discount_code = '' )— Whether the user has used the code.format_discount( $amount = '', $type = '' )— Prefix%for percentage discounts, or add the currency sign for flat rates.calc_discounted_price( $base_price = '', $discount_amount = '', $type = '%' )— Calculate the discounted price from a base price, amount, and type (%orflat).