Accept Orders Only During Opening Hours
Disables checkout outside your opening hours and shows a notice with the time you open again. Ideal for restaurants and local delivery.
Opens WPIDE on
Code
$wpide_opening_hours = array(
// Day number => array( open, close ) in 24h format.
1 => array( '09:00', '21:00' ),
2 => array( '09:00', '21:00' ),
3 => array( '09:00', '21:00' ),
4 => array( '09:00', '21:00' ),
5 => array( '09:00', '22:00' ),
6 => array( '10:00', '22:00' ),
);
$wpide_store_is_open = function () use ( $wpide_opening_hours ) {
$now = new DateTime( 'now', wp_timezone() );
$day = (int) $now->format( 'N' );
if ( empty( $wpide_opening_hours[ $day ] ) ) {
return false;
}
$time = $now->format( 'H:i' );
return $time >= $wpide_opening_hours[ $day ][0] && $time < $wpide_opening_hours[ $day ][1];
};
add_action( 'woocommerce_check_cart_items', function () use ( $wpide_store_is_open ) {
if ( ! $wpide_store_is_open() ) {
wc_add_notice( 'We are closed right now. You can browse and fill your cart, and place your order during our opening hours.', 'error' );
}
} );
add_action( 'woocommerce_checkout_process', function () use ( $wpide_store_is_open ) {
if ( ! $wpide_store_is_open() ) {
wc_add_notice( 'Sorry, we are not taking orders at the moment.', 'error' );
}
} );
Notes
Times use the site timezone from Settings > General. Set $open and $close per weekday (1 = Monday ... 7 = Sunday); leave a day out to close all day.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.