Skip to content

Instantly share code, notes, and snippets.

@saranyagokula
Last active February 5, 2024 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saranyagokula/bcbcb628f726ab71c149fc0a36c36d59 to your computer and use it in GitHub Desktop.
Save saranyagokula/bcbcb628f726ab71c149fc0a36c36d59 to your computer and use it in GitHub Desktop.
Customizing Estimated Delivery Date and Time for Unique Business Scenarios
add_action('woocommerce_after_add_to_cart_form', 'ts_dispatch_info_single_product');
function ts_dispatch_info_single_product() {
// Get the current time according to WordPress timezone settings
$current_time = current_time('mysql');
// Convert the current time to a DateTime object
$current_datetime = new DateTime($current_time);
// Get the current day of the week (1 for Monday, 7 for Sunday)
$current_day = (int)$current_datetime->format('N');
// Get the current hour of the day (0 to 23)
$current_hour = (int)$current_datetime->format('G');
// Get the product ID
$product_id = get_the_ID();
// Get the product object
$product = wc_get_product($product_id);
// Check if the product is in stock
if ($product && $product->is_in_stock()) {
// Set default values for delivery day and order_by
$del_day = '';
$order_by = '';
// Check for Monday to Friday orders before 16:00
if ($current_day >= 1 && $current_day <= 5 && $current_hour < 16) {
$del_day = date("jS F", strtotime("tomorrow"));
$order_by = "tomorrow";
}
// Check for Saturday and Sunday orders
elseif ($current_day == 6 || $current_day == 7) {
$del_day = date("jS F", strtotime("next Tuesday"));
$order_by = "next Tuesday";
}
// Generate HTML message
if (!empty($order_by)) {
$html = "<br><div class='woocommerce-message' style='clear:both'>Order by 4 PM to get delivered by {$order_by} on {$del_day}</div>";
// Display the HTML message
echo $html;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment