'Conditional Delivery Notice based on Time and Date in Woocommerce
I am trying to get a message together that will display a shipping notice depending on what time it is monday to thursday and friday to sunday.
If the customer orders before 12 o'clock (daytime) the order is shipped the same day, monday to thursday and delivered the next day.
If the customer orders after 12 o'clock monday to thursday the order will be prepared and shipped the day after, monday to thursday.
All orders made friday to sunday will be prepared and shipped on the following weekday (monday).
The code I am using does not do this and I am trying to understand how to make it work. Any help is very much appreciated.
add_action( 'woocommerce_before_customer_login_form', 'next_day_delivery' );
add_action( 'woocommerce_before_customer_login_form', 'next_day_delivery' );
add_action( 'woocommerce_before_checkout_form', 'next_day_delivery' );
add_action( 'woocommerce_before_shop_loop', 'next_day_delivery' );
add_action( 'woocommerce_before_single_product_summary', 'next_day_delivery' );
add_action( 'woocommerce_before_cart', 'next_day_delivery' );
function next_day_delivery() {
date_default_timezone_set('Europe/Stockholm');
$end_time = mktime('12', '00', '00', '2018');
$now_time = strtotime("now");
if ( WC()->cart->get_cart_contents_count() > 0 ) && $now_time < $end_time {
// print the information notice
wc_print_notice( __( 'Order within $end_time - $now_time and get your order delivered tomorrow!', 'woocommerce' ), 'success' );
}
else if wc_print_notice( __( 'Your order will be prepared and shipped on Monday.', 'woocommerce' ), 'success' );
}
Solution 1:[1]
There is some mistakes and missing things in your code. The following will display conditionally a custom shipping notice as defined in your question:
add_action( 'woocommerce_before_customer_login_form', 'next_day_delivery' );
add_action( 'woocommerce_before_customer_login_form', 'next_day_delivery' );
add_action( 'woocommerce_before_checkout_form', 'next_day_delivery' );
add_action( 'woocommerce_before_shop_loop', 'next_day_delivery' );
add_action( 'woocommerce_before_single_product_summary', 'next_day_delivery' );
add_action( 'woocommerce_before_cart', 'next_day_delivery' );
function next_day_delivery() {
if( WC()->cart->is_empty() )
return; // Exit
// Set the time zone
date_default_timezone_set('Europe/Stockholm');
// From Monday to Thursday
$is_week_days = in_array( date('w'), array( 1, 2, 3, 4 ) ) ? true : false;
$end_time = mktime('12', '00', '00', date('m'), date('d'), date('Y'));
$now_time = time();
$after_tomorow = date('l', strtotime('+2 days'));
$dateDiff = intval(($end_time - $now_time)/60);
$diff_hours = intval($dateDiff/60);
$diff_minutes = $dateDiff%60;
$hours_label = _n( 'hour', 'hours', $diff_hours, 'wooocommerce' );
$minutes_label = _n( 'minute', 'minutes', $diff_minutes, 'wooocommerce' );
if ( $end_time > $now_time && $is_week_days ) {
// print the information notice
$message = sprintf( __( '%s left to be delivered tomorrow!', 'woocommerce' ),
$diff_hours.' '.$hours_label.' and '.$diff_minutes.' '.$minutes_label);
}
elseif ( $end_time <= $now_time && $is_week_days ) {
$message = sprintf( __( 'Your order will be delivered this %s.', 'woocommerce' ), $after_tomorow );
} else {
$message = __( 'Your order will be prepared and shipped next upcoming monday and delivered on tuesday.', 'woocommerce' );
}
wc_print_notice( $message, 'success' );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |