The 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) - Either 'active' or 'inactive'. Default is inactive.
unit(string) - Either 'flat' or '%'. Default is '%'.
code(string) - Discount code.
expiration(string) - Date the discount expires in Y-m-d format. Default is empty string (no expiration).
max_uses(int) - Maximum number of uses. Use 0 for unlimited.
use_count(int) - Number of times the code has been used. Default is 0.
subscription_id(int) - Associated subscription ID. Default is 0 (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 ) - Get the status of a discount.
- get_amount( $discount_id = 0 ) - Get the discounted amount.
- get_uses( $discount_id = 0 ) - The number of times a discount has been used.
- get_max_uses( $discount_id = 0 ) - The maximum number of times a discount can be used.
- get_subscription_id( $discount_id = 0 ) - The associated subscription level for a discount.
- has_subscription_id( $discount_id = 0 ) - Checks whether a discount code has a subscription associated with it.
- increase_uses( $discount_id = 0 ) - Increase the use count of a discount by one.
- get_expiration( $discount_id = 0 ) - Get the expiration date of a discount.
- get_type( $discount_id = 0 ) - Get the type of discount (will be 'flat' or '%').
- delete( $discount_id = 0 ) - Delete a specific discount from the database.
- is_maxed_out( $discount_id = 0 ) - Whether or not a given discount code has reached its max uses (true or false).
- is_expired( $discount_id = 0 ) - Checks whether a discount code is expired.
- add_to_user( $user_id = 0, $discount_code = '' ) - Add a discount code to a user's history.
- user_has_used( $user_id = 0, $discount_code = '' ) - Check if a user has used a discount code.
- format_discount( $amount = '', $type = '' ) - Format a discount code. If discount is percentage based, a % symbol is prefixed to the amount. If a code is a flat rate, the currency sign is added.
- calc_discounted_price( $base_price = '', $discount_amount = '', $type = '%' ) - Calculate the discounted price given a base price (float), a discount amount (float), and a discount type ('%' or 'flat').