Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.