Search code examples
phpwordpresswoocommercedate-rangecoupon

Coupon daily time range in WooCommerce


I'm tying to enable the use of coupons to a range of hours in Woocommerce without success.

Based on Discount on specific products based on a daily time range in Woocommerce answer, my code is:

// Utility function that gives the discount daily period
function get_discount_period_rate(){
    // Set the correct time zone  (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/Paris');


    // Set the start time and the end time
    $start_time = mktime( 08, 00, 00, date("m")  , date("d"), date("Y") );
    $end_time   = mktime( 09, 00, 00, date("m")  , date("d"), date("Y") );
    $time_now   = strtotime("now");
}

    // Set the coupon Ids that will be discounted
  $wc_coupon = new WC_Coupon('integralia10'); // get intance of wc_coupon which code is "integralia10"
    if (!$wc_coupon || !$wc_coupon->is_valid()) {
        return;
    }

    $coupon_code = $wc_coupon->get_code();
    if (!$coupon_code) {
        return;
    }

Also, I Would like to use the wc_print_notices function to show a message when somemeone try to use the Coupon code out of time range.

Any Sugestion?


Solution

  • With the following code, all coupons that fall within a certain time frame will be valid, if not, an error message will be displayed.

    Please set:

    • The correct time zone
    • The start and end time
    function time_range() {
        // Set the correct time zone (http://php.net/manual/en/timezones.php)
        date_default_timezone_set( 'Europe/Brussels' );
    
        // Set the start time and the end time to be valid
        $start_time = mktime( 11, 00, 00, date( 'm' ), date( 'd' ), date( 'y' ) );
        $end_time   = mktime( 18, 00, 00, date( 'm' ), date( 'd' ), date( 'y' ) );
        $time_now   = strtotime( 'now' );
        
        // Return true or false
        return $start_time <= $time_now && $end_time >= $time_now ? true : false;
    }
    
    // Is valid
    function filter_woocommerce_coupon_is_valid( $valid, $coupon, $discount ) {
        // Call function, return true or false
        $valid = time_range();
    
        // NOT valid
        if ( ! $valid ) {
            throw new Exception( __( 'My custom error message.', 'woocommerce' ), 109 );
        }
    
        return $valid;
    }
    add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );
    

    UPDATE: To apply the same, but only to certain coupons ID's, use this instead.

    function time_range_coupon_id( $coupon_id ) {
        // For specific coupon ID's only, several could be added, separated by a comma
        $specific_coupons_ids = array( 107, 108 );
        
        // Coupon ID in array, so check
        if ( in_array( $coupon_id, $specific_coupons_ids ) ) {
            // Set the correct time zone (http://php.net/manual/en/timezones.php)
            date_default_timezone_set( 'Europe/Brussels' );
    
            // Set the start time and the end time to be valid
            $start_time = mktime( 12, 00, 00, date( 'm' ), date( 'd' ), date( 'y' ) );
            $end_time   = mktime( 17, 00, 00, date( 'm' ), date( 'd' ), date( 'y' ) );
            $time_now   = strtotime( 'now' );
            
            // Return true or false
            return $start_time <= $time_now && $end_time >= $time_now ? true : false;
        }
        
        // Default
        return true;
    }
    
    // Is valid
    function filter_woocommerce_coupon_is_valid( $valid, $coupon, $discount ) {
        // Get coupon ID
        $coupon_id = $coupon->get_id();
        
        // Call function, return true or false
        $valid = time_range_coupon_id( $coupon_id );
    
        // NOT valid
        if ( ! $valid ) {
            throw new Exception( __( 'My error message', 'woocommerce' ), 109 );
        }
    
        return $valid;
    }
    add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );