mycred_use_coupon
Description
This function will validate and a given coupon code and if the coupon can be used, redeems the coupon.
This function is only available if the Coupons add-on is enabled.
Available since version 1.0
Returns
(int | float | bool | string) Returns FALSE
if the coupon can not be used, a string if the coupon is not valid or the users balance after the coupon was applied if the coupon is valid and was successfully used.
Error Codes
You can use the mycred_get_coupon_error_message function to convert an error code into a readable message.
Error Code | Description |
---|---|
invalid |
The coupon code or user ID is invalid. Remember that codes are case sensitive. |
expired |
The coupon has expired. This occurs if an expiration date has been reached or the maximum usage limit has been reached. |
user_limit |
User limit reached. |
min |
The user does not meet the minimum balance requirement set for this coupon. |
max |
The user does not meet the maximum balance requirement set for this coupon. |
excluded |
The user is excluded from the point type that the coupon is set to payout. |
Parameters
Param | Type | Required | Description | |
---|---|---|---|---|
function mycred_use_coupon( |
||||
$code |
string | Yes | The coupon code. |
|
$user_id |
int | Yes | A numeric user ID that will receive the points if the coupon is valid. |
|
) { ... } |
Examples
Example 1: Apply a posted coupon code to the current user.
$user_id = get_current_user_id(); $code = $_POST['coupon']; $results = mycred_use_coupon( $code, $user_id ); if ( mycred_coupon_was_successfully_used( $results ) ) { // Coupon was successfully used } else { // Something went wrong }