A lot of online stores allow free delivery before a certain time of day depending on the courier used. This guide shows you how you can enable the shipping method, free shipping between midnight and 2PM in the afternoon. After 2PM, the free shipping option is turned off. If you need this solution implemented but between different times, simply the times in the code as necessary. You will need the shipping method radio button value which you can get by using your browser’s develop tools to inspect the radio button li class and grabbing the value. You’ll also need to clear customer sessions by going to Dashboard > WooCommerce > Status > Tools > Clear customer sessions. Because of this, it’s a good idea to implement this solution when there is little traffic on the website as clearing custom sessions will empty their cart.
/**
* Snippet Name: WooCommerce disable a shipping method after a certain time. In this case, disable free shipping after 2PM and enable it at midnight.
* Snippet Author: ecommercehints.com
*/
add_filter( 'woocommerce_package_rates', 'ecommercehints_enable_shipping_method_based_on_coupon', 10, 2 );
function ecommercehints_enable_shipping_method_based_on_coupon($rates, $package) {
$current_time = date("h:i a");
$begin = "2:00 pm"; // Disable the shipping method after this time
$end = "11:59 pm"; // Enable the shipping method after this time
$date1 = DateTime::createFromFormat('H:i a', $current_time);
$date2 = DateTime::createFromFormat('H:i a', $begin);
$date3 = DateTime::createFromFormat('H:i a', $end);
if ($date1 > $date2 && $date1 < $date3) {
unset( $rates['free_shipping:14'] ); // The shipping method radio button value
}
return $rates;
}
How Can I Get The Shipping Method Radio Button Value?
Use your browser’s dev tools to inspect the radio button and grab the value from the li class as shown in the screenshot.
