Skip to content

Instantly share code, notes, and snippets.

@xadapter
Last active September 20, 2019 13:57
Show Gist options
  • Select an option

  • Save xadapter/d8caee4845bb41c0d3da1a9b2a3ba897 to your computer and use it in GitHub Desktop.

Select an option

Save xadapter/d8caee4845bb41c0d3da1a9b2a3ba897 to your computer and use it in GitHub Desktop.
/**
* Snippet to hide woocommerce shipping methods based on days, based on cutoff time. Drawback - If Calculated before Cutoff time and placed order after cutoff then it won't work.
* Created at : 16 July 2018
* Updated at : 02 Aug 2018
* PluginHive Plugins : https://www.pluginhive.com/plugins/
* Gist Link : https://gist.github.com/xadapter/d8caee4845bb41c0d3da1a9b2a3ba897
*/
add_filter( 'woocommerce_package_rates', function( $shipping_rates) {
// To hide shipping methods based on days
$shipping_method_to_hide_after_time = array(
'17:00' => array(
"wf_fedex_woocommerce_shipping:FEDEX_GROUND_TEST", "flat_rate:1test" // Time in 24 hour format and array of shipping methods array
),
'15:30' => array(
"wf_fedex_woocommerce_shipping:FEDEX_GROUND_EST", "flat_rate:test", "flat_rate:1", "free_shipping:2"
),
);
$message_to_display_based_on_time = "[SERVICES] Services are Not Available on Weekends or After [TIME] Weekdays."; // Leave empty to not display any message, Tags available - [SERVICES], [TIME]
//To hide shipping methods based on day
$hide_shipping_methods_based_on_day = array(
'Wed' => array(
'flat_rate:1',
),
'Thu' => array(),
);
$message_to_display_for_days = "[SERVICES] not available on [DAYS]."; //Leave empty to not display any messag, Tage available - [SERVICES], [DAYS]
$wp_timezone_string = get_option('timezone_string');
$wf_date = new DateTime;
if( empty($wp_timezone_string) ) {
$time_offset = get_option('gmt_offset');
$wp_timezone_string = timezone_name_from_abbr( "", $time_offset*60*60, 0 );
}
$date_obj = new DateTime( "now", new DateTimeZone( $wp_timezone_string )); // Current time and date
// Handle based on days
$today_name = $date_obj->format('D'); // Day as first three letter like Sun
$today_name_full = $date_obj->format('l'); // Complete day name like Sunday
if( isset( $hide_shipping_methods_based_on_day[$today_name]) ) {
foreach( $shipping_rates as $key => $shipping_rate ) {
$shipping_method_id = $shipping_rate->get_id();
if( in_array($shipping_method_id, $hide_shipping_methods_based_on_day[$today_name]) ) {
$services_hidden = ! empty($services_hidden) ? $services_hidden.', '.$shipping_rate->get_label() : $shipping_rate->get_label();
unset($shipping_rates[$key]);
}
}
if( ! empty($services_hidden) && ! empty($message_to_display_for_days) ) {
$message = str_replace( "[SERVICES]", $services_hidden, $message_to_display_for_days );
$message = str_replace( "[DAYS]", $today_name_full, $message );
if( is_cart() || is_checkout() ) {
wc_add_notice(print_r($message,true), "error" );
}
}
}
// Handle based on current time
$current_time = $date_obj->format('H:i');
foreach( $shipping_method_to_hide_after_time as $time => $shipping_methods_to_hide ){
$services = null;
if( $current_time > $time ) {
foreach( $shipping_rates as $key => $shipping_rate ) {
$shipping_method_id = $shipping_rate->get_id();
if( in_array($shipping_method_id, $shipping_methods_to_hide) ) {
$services = ! empty($services) ? $services.', '.$shipping_rate->get_label() : $shipping_rate->get_label();
unset($shipping_rates[$key]);
}
}
}
if( ! empty($services) && ! empty($message_to_display_based_on_time) ) {
$message = str_replace( "[SERVICES]", $services, $message_to_display_based_on_time );
$exploded_time = explode( ':', $time );
$time_as_12_hr_format = ( (int)$exploded_time[0] > 12 ) ? ( (string) ( (int)$exploded_time[0] - 12) ).':'.$exploded_time[1]. ' pm' : $time.' am';
$message = str_replace( "[TIME]", $time_as_12_hr_format, $message );
if( is_cart() || is_checkout() ) {
wc_add_notice(print_r($message,true), "error" );
}
}
}
return $shipping_rates;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment