Hooks
modal_kit_pro_coupon_issued
action
Pro
PHP
Fires after a game creates a unique WooCommerce coupon for a prize. Add restrictions, log it or pass it on.
Runs on the live site during a play (POST modal-kit-pro/v1/play), right after Pro saves a single-use coupon for a prize whose reward is a WooCommerce coupon, and before the visitor gets the result.
The coupon already has its code, discount, a usage limit of 1, an expiry and, when the game allows one play per email, the player’s address as an email restriction. Its meta records the modal (_modal_kit_game), the game (_modal_kit_game_id) and the prize (_modal_kit_prize).
Parameters
Parameter
Description
$code
string
The coupon code: the game’s prefix, a dash and ten random characters.
$prize
array
The prize from the game block: id, label, chance and reward (coupon), plus, when set, discountType (percent, fixed_cart or free_shipping), amount, expiryHours, symbol and message.
$modal_id
int
The modal’s post ID.
Examples
Exclude sale items and set a minimum spend
add_action( 'modal_kit_pro_coupon_issued', function ( $code ) {
$coupon = new WC_Coupon( $code );
if ( ! $coupon->get_id() ) {
return;
}
$coupon->set_exclude_sale_items( true );
$coupon->set_minimum_amount( 30 );
$coupon->save();
} );
Log each prize in WooCommerce
The entries appear under WooCommerce → Status → Logs.
add_action(
'modal_kit_pro_coupon_issued',
function ( $code, $prize, $modal_id ) {
wc_get_logger()->info(
sprintf(
'Coupon #%d (%s) issued by "%s"',
wc_get_coupon_id_by_code( $code ),
$prize['label'],
get_the_title( $modal_id )
),
array( 'source' => 'my-plugin-prizes' )
);
},
10,
3
);
Good to know
Not fired for fixed codes (reward code), when WooCommerce isn’t active, or when the game’s daily coupon limit is used up. In the last two cases the visitor sees “This prize isn’t available right now.”
It runs inside the visitor’s request, so keep it quick. Queue slow work, such as a call to a CRM, with Action Scheduler or a cron event.
Pro deletes a game’s coupons that were never used 7 days after they expire.
Fired in modal-kit-pro/includes/Games.php.
Related