Home / eCommerce / Block specific coupons during set hours
Duplicate Snippet

Embed Snippet on Your Site

Block specific coupons during set hours

Code Preview
php
<?php
/**
 * Block specific coupons during set hours (1:00 PM - 3:00 PM)
 */
function block_coupon_during_specific_hours($valid, $coupon) {
    if (!$valid) return $valid;
    
    // List of coupon codes to block during specific hours
    $blocked_coupons = array(
        'YOUR_COUPON_CODE_HERE', // Replace with your specific coupon code
        // Add more coupon codes here if needed
    );
    
    // If the current coupon is not in our block list, return valid
    if (!in_array($coupon->get_code(), $blocked_coupons)) {
        return $valid;
    }
    
    // Get current time in 24-hour format
    $current_time = current_time('H:i');
    $block_start = '13:00'; // 1:00 PM
    $block_end = '15:00';   // 3:00 PM
    
    // Check if current time is within blocked period
    if ($current_time >= $block_start && $current_time <= $block_end) {
        throw new Exception(sprintf(
            __('Coupon code "%s" is not available between 1:00 PM and 3:00 PM.', 'woocommerce'),
            $coupon->get_code()
        ));
    }
    
    return $valid;
}
// Add this validation after the plugin's time availability check
add_filter('woocommerce_coupon_is_valid', 'block_coupon_during_specific_hours', 15, 2);

Comments

Add a Comment