Skip to content

Instantly share code, notes, and snippets.

@luizbills
Created February 14, 2024 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizbills/9ce6254d956942639b9498730cb71f1c to your computer and use it in GitHub Desktop.
Save luizbills/9ce6254d956942639b9498730cb71f1c to your computer and use it in GitHub Desktop.
Make a woocommerce coupon valid only in certain period of the day
<?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