Created
February 14, 2024 16:58
-
-
Save luizbills/9ce6254d956942639b9498730cb71f1c to your computer and use it in GitHub Desktop.
Make a woocommerce coupon valid only in certain period of the day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param bool $is_valid | |
* @param \WC_Coupon $coupon Coupon data. | |
* @return bool | |
*/ | |
add_filter( 'woocommerce_coupon_is_valid', function ( $is_valid, $coupon ) { | |
if ( ! $is_valid ) return false; | |
$coupon_code = 'XXXXXX'; | |
$start = 2300; // 23:00 | |
$end = 500; // 5:00 | |
if ( mb_strtolower( $coupon->get_code() ) === mb_strtolower( $coupon_code ) ) { | |
$current_time = (int) \wp_date( 'Gi' ); | |
$is_valid = $current_time >= $start || $current_time <= $end; | |
} | |
return $is_valid; | |
}, 20, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment