This function creates a new myCRED Coupon post object. Uses wp_insert_post.
(int | WP_Error) Returns either the coupon post object ID or a WP_Error object.
Param | Type | Required | Description | |
---|---|---|---|---|
function mycred_create_new_coupon( |
||||
$args |
array | Yes | Associative array of coupon arguments. |
|
) { ... } |
Argument | Type | Required | Description |
---|---|---|---|
code |
string | No | The coupon code. If not set, the function will generate a random code. |
value |
int or float | Yes | The amount of points this coupon generates when redeemed. |
type |
string | No | The point type that this coupon generates. Defaults to the default type key. |
global_max |
int | No | The maximum number of times the coupon can be redeemed. Defaults to 1. Can not be zero! |
user_max |
int | No | The maximum number of times the coupon can be redeemed by a single user. Defaults to 1. |
min_balance |
int or float | No | Optional minimum balance requirement for redeeming this coupon. Users who have less points then this value will not be able to redeem this coupon. |
min_balance_type |
string | No | Optional point type key for the minimum balance requirement. |
max_balance |
int or float | No | Optional maximum balance requirement for redeeming this coupon. Users who have more points then this value will not be able to redeem this coupon. |
max_balance_type |
string | No | Optional point type key for the maximum balance requirement. |
expires |
int | No | Optional expiration date for the coupon. Uses strtotime to convert the date into a UNIX timestamp. Do not use if the coupon never expires / expires when used up. |
Example 1: Create a new coupon that can be used 10 times in total and gives 5 points when redeemed.
$args = array( 'value' => 5, 'global_max' => 10 ); $new_coupon = mycred_create_new_coupon( $args ); if ( $new_coupon !== false && ! is_wp_error( $new_coupon ) ) { // Coupon was successfully created }
Example 2: Create a new coupon called “XMASOFFER“ that gives 100 points when redeemed. In order to use the coupon, the user must have less than 100 points. Make sure the coupon can not be used more than 500 times.
$args = array( 'code' => 'XMASOFFER' 'value' => 100, 'global_max' => 500, 'max_balance' => 100 ); $new_coupon = mycred_create_new_coupon( $args ); if ( $new_coupon !== false && ! is_wp_error( $new_coupon ) ) { // Coupon was successfully created }