Allow Shipping Methods Based on Date and Time

In this article, I will show you different ways to enable or disable WooCommerce shipping methods based on:

Below, I will guide you on how to implement each option both programmatically and using my plugin, Simple Conditional Shipping and Payments.

As a bonus, we will also calculate shipping costs based on date and time.

Disable a Custom Flat Rate Shipping in a Specific Time of Day

Let’s start with the plugin approach first.

If you’re about to use the Simple Conditional Shipping and Payments plugin, then everything you need to do is create a condition in the plugin settings. To do so, we need to navigate to WooCommerce > Settings > Conditions > Shipping Conditions. And then, just hit the “Add shipping condition” button there. After that, use “Time of day” as a condition and “Disable shipping methods” as an action.

Something like that:

Disable shipping method based on time of day

Just click the “Add rule” button, and that’s pretty much it; it is fully configured.

In case you were searching for a programmatic approach, you might want to use a WooCommerce standard filter hook for that – woocommerce_package_rates.

/**
 * @snippet Disable shipping method based on time of the day
 * @author Misha Rudrastyh
 * @url https://rudrastyh.com/woocommerce/shipping-methods-based-on-date-and-time.html#time-of-day
 */
add_filter( 'woocommerce_package_rates', 'rudr_disable_shipping_by_time', 25 );

function rudr_disable_shipping_by_time( $rates ) {
	// let's get the current date in a timezone which our WooCommercestore is using
	$now = new DateTime( 'now', wp_timezone() );
	// we need only the hour here
	$hour = $now->format( 'G' ); // 0–23

	// it is time to disable a shipping method 0:00 and 5:00
	if( $hour < 5 ) {
		foreach( $rates as $rate_id => $rate ) {
			if( 'flat_rate:4' === $rate_id ) {
				unset( $rates[ $rate_id ] );
			}
		}
	}

	return $rates;
}

The snippet is quite simple; however, using the plugin has its advantages, like keeping all the configuration in one place and in order. Plus, the plugin neither slows down your store in any way nor overloads it with notifications or anything.

Allow Free Shipping on Specific Days of the Week

Now, let’s continue to the second example. Let’s say we would like to allow customers to select free shipping on specific days of the week, for example, on the weekend.

How can we do it?

Again, it can be easily achieved with my Simple Conditional Shipping and Payments plugin. To do it, let’s open the WooCommerce > Settings > Conditions > Shipping Conditions settings page and then create a new condition there.

An appropriate condition will look like this:

WooCommerce how to allow free shipping only on specific days of the week

Here is how you can do it programmatically:

/**
 * @snippet Allow free shipping methods on the weekend
 * @author Misha Rudrastyh
 * @url https://rudrastyh.com/woocommerce/shipping-methods-based-on-date-and-time.html#day-of-the-week
 */
add_filter( 'woocommerce_package_rates', 'rudr_free_shipping_on_weekend', 25 );

function rudr_free_shipping_on_weekend( $rates ) {
	// let's get the current date in a timezone which our WooCommerce store is using
	$now = new DateTime( 'now', wp_timezone() );
	// we need only the day of the week
	$day = $now->format( 'N' ); // 1 (Mon) - 7 (Sun)

	// it is time to enable free shipping only on the weekend
	// or, in other words, we need to disable it for all the other days of the week
	if( ! in_array( $day, array( 6, 7 ) ) ) {
		foreach( $rates as $rate_id => $rate ) {
			if( 0 === strpos( $rate_id, 'free_shipping:' ) ) {
				unset( $rates[ $rate_id ] );
			}
		}
	}

	return $rates;
}

Bonus. Custom Shipping Costs Based on Date and Time

Before we continue straight to the solution, I’d like to remind you that it is always possible to create two similar “Flat rate” shipping methods with different shipping costs, and enable one method at a specific date and time, while disabling the second one.

At this moment, I think I can not suggest a plugin solution for that, because I don’t have one; however, I’m currently considering developing it, and shipping method settings will look something like this:

WooCommerce conditional shipping costs
So, when editing a specific shipping method, you can configure conditional costs at the same place.

What would you say? Would you be interested in a plugin like this? If yes, please let me know in the comments section below.

Still, it is possible to do this using a programmatic approach; specifically, we will need to use the woocommerce_package_rates hook once again; then the code snippet will be:

/**
 * @snippet Custom shipping costs on the weekend
 * @author Misha Rudrastyh
 * @url https://rudrastyh.com/woocommerce/shipping-methods-based-on-date-and-time.html#shipping-cost-by-date-and-time
 */
add_filter( 'woocommerce_package_rates', 'rudr_custom_shipping_cost_on_the_weekend', 25 );

function rudr_custom_shipping_cost_on_the_weekend( $rates ) {
	// let's get the current date in a timezone which our WooCommerce store is using
	$now = new DateTime( 'now', wp_timezone() );
	// we need only the day of the week
	$day = $now->format( 'N' ); // 1 (Mon) - 7 (Sun)

	if( in_array( $day, array( 6, 7 ) ) ) {
		// it is a weekend! time to increase the prices
		foreach( $rates as $rate_id => &$rate ) {
			if( 0 === strpos( $rate_id, 'flat_rate:' ) ) {
				// increase the costs
				$rate->cost += 100;
			}
		}
	}

	return $rates;
}
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X