Does your store want to offer a specific shipping option to be available only after a certain time of the day(e.g. after 4 PM)? This code will be useful for stores that need time to prepare orders for pickup and want to ensure that customers can only choose this option during specific hours.
add_filter( 'woocommerce_package_rates', 'ts_hide_shipping_method_based_on_time', 10, 2 ); function ts_hide_shipping_method_based_on_time( $rates, $package ) { // Set your default time zone (http://php.net/manual/en/timezones.php) date_default_timezone_set('Asia/Kolkata'); // Set the timezone to India Standard Time (IST) // Here set your shipping rate Id $shipping_rate_id = 'local_pickup:6'; // When this shipping method is available and after 4 PM if ( array_key_exists( $shipping_rate_id, $rates ) && date('H') > 16 ) { unset( $rates[$shipping_rate_id] ); // remove it } return $rates; }
Output
As specified in this code, the ‘Local Pickup’ shipping method will be hidden if the customer is checking out before 4PM. Adjusting the condition date('H') >=
4 allows for customization of the time after which the ‘Local Pickup’ method becomes available during the checkout process.
The local pickup option is available for customers only after 4PM, since the time is specified in the code.
Similar to the above functionality, we can also hide WooCommerce shipping methods for certain conditions based on order weight, product total, product quantity, etc.