mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/76851576/reset-coupon-usage-limit-after-a-specified-time-in-woocommerce/76852090
15 lines
753 B
Text
15 lines
753 B
Text
add_action( 'woocommerce_coupon_options_save', 'trigger_coupon_schedule_single_event', 10, 2 );
|
|
function trigger_coupon_schedule_single_event( $post_id, $coupon ) {
|
|
// Check that some usage limit has been activated for the current coupon
|
|
if ( $coupon->get_usage_limit() || $coupon->get_usage_limit_per_user() ) {
|
|
// Create a shedule event on 'coupon_schedule_reset_restrictions' custom hook
|
|
wp_schedule_single_event( time() + 60, 'coupon_schedule_reset_restrictions', array( $coupon ) );
|
|
}
|
|
}
|
|
|
|
add_action( 'coupon_schedule_reset_restrictions', 'coupon_reset_restrictions' );
|
|
function coupon_reset_restrictions( $coupon ){
|
|
$coupon->set_usage_limit(null);
|
|
$coupon->set_usage_limit_per_user(null);
|
|
$coupon->save();
|
|
}
|