Using php to restrict delivery options in woocommerce based on time

Hi folks. I’ve cobbled together a php snippet with the goal of removing all delivery options from the checkout form unless the time is between 1200 and 1945.

Here are the resources I’ve used to put my snippet together
How To Disable WooCommerce Shipping Methods for Certain Products – AVADA Commerce
php - Show and Hide div based on day and time - Stack Overflow

Here is my snippet

function lf_hide_shipping_for_closed_hours( $rates ) {

    $currentTime = date("Gi");

    $deliveryStart = 1200;
    $deliveryEnd = 1945;
	$condition = $currentTime >= $deliveryStart && $currentTime < $deliveryEnd;

    // Check if we are open
	foreach ( $rates as $rate_id => $rate_val ) {
		if ( $condition ){
			if ( 'local_pickup:2' === $rate_val->get_method_id() ) {
	return $rates;

add_filter( 'woocommerce_package_rates', 'lf_hide_shipping_for_closed_hours', 100, 2 );

It doesn’t work, but if I remove the “return $rates;” line it nerfs all my ordering options.

I’ve played with it quite a bit, some insight would be appreciated.

This is a for a wordpress woocommerce website.

I think my issue might have something to do with a plugin I’m using called shipping zones by drawing. The $rate_val ids I would like restricted are “szbd-shipping-method:5”, “szbd-shipping-method:6”, “szbd-shipping-method:7”, and “szbd-shipping-method:8” which amount to our local delivery zones.

Any input or resources appreciated. Would like to avoid paying ~$100 a year for a plugin for this functionality.

The current logic removes the ‘local_pickup:2’ rate/choice if you are open ($condition is true.) So, at a minimum, you need to add ! (not) to the logic testing $condition, so that you do ‘something’ when you are not open.

However, based on your statement of what you want, if you are open you don’t want to remove anything? You do want to either remove specific rates/choices or you in fact want to remove all rates/choices if you are not open? Is this a correct interpretation of the operation you want? If so, all you really need to do is return $rates if you are open, and return, probably an empty array, if you are closed.

Since a true $condition actually means ‘open’, perhaps call it $open, so that when you read the logic, it tells you directly what it is doing. Therefore, !$open, would mean not open.

Sponsor our Newsletter | Privacy Policy | Terms of Service