Estimated delivery date

Code Explanation

  1. The add_action function hooks the ts_dispatch_info_single_product function to the woocommerce_after_add_to_cart_form action. This means the function will be executed after the add-to-cart form on the product page.
  2. The function current_time(‘mysql’) retrieves the current time based on the WordPress timezone settings. This is done to ensure that the code works correctly regardless of the server’s timezone.
  3. The retrieved current time is then converted into a DateTime object named $current_datetime.
  4. The code extracts the current day of the week (1 for Monday through 7 for Sunday) and the current hour of the day (0 to 23) from the object.
  5. If the current day is Friday, Saturday, or Sunday ($current_day >= 5), the estimated delivery date will be the next Monday, and the order message will indicate “Monday.” The day starts with 0 as Sunday, 1 as Monday, 2 as Tuesday, 3 as Wednesday, 4 as Thursday, 5 as Friday, 6 as Saturday, and 7 as Sunday.
  6. If the current day is Monday, Tuesday, or Wednesday ($current_day >= 2 && $current_day <= 4), and the current hour is 4 PM or later ($current_hour >= 16), the estimated delivery date will be tomorrow, and the order message will indicate “tomorrow.”
  7. If none of the above conditions are met, it means the current day is Monday, Tuesday, or Wednesday, and it’s before 4 PM. In this case, the estimated delivery date will be today, and the order message will indicate “today.”
  8. The HTML message is prepared based on the calculated delivery date and order information.
  9. Finally, the HTML message is echoed out, and it will be displayed on the product page to inform customers about the estimated delivery date based on the current day and time.

Solution: show the estimated delivery date or dispatch time on the WooCommerce product page

Let’s consider an example of an Electronic Retail Store that determines the estimated delivery date or dispatch time based on the current time and day. And then it displays a message about when the order needs to be placed for different delivery options.

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');

    // if FRI/SAT/SUN delivery will be MON
    if ($current_day >= 5) {
        $del_day = date("l jS F", strtotime("next monday"));
        $order_by = "Monday";
    }

    // if MON/THU after 4 PM delivery will be TOMORROW
    elseif ($current_day >= 2 && $current_day <= 4 && $current_hour >= 16) {
        $del_day = date("l jS F", strtotime("tomorrow"));
        $order_by = "tomorrow";
    }

    // if MON/THU before 4 PM delivery will be TODAY
    else {
        $del_day = date("l jS F", strtotime("today"));
        $order_by = "today";
    }

    $html = "<br><div class='woocommerce-message' style='clear:both'>Order by 4 PM {$order_by} for delivery on {$del_day}</div>";

    echo $html;
}

Customizing Estimated Delivery Date and Time for Unique Business Scenarios

Our previous code included logic to determine the delivery day based on the current day of the week and the time of the order placement. We’ve refined this logic to better accommodate store owner’s real expectations.

The code given below adds a custom message to the product page based on the current date, day of the week, time, and specific business rules (Monday to Friday before 4 PM for next day delivery, Saturday and Sunday orders for delivery on the next Tuesday).

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');

    // 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;
    }
}

Output

Let’s understand the output in different scenarios:Scenario 1: Order placed on Monday to Friday before 4 PM

  • Current Day: Tuesday
  • Current Date: 1/30/2024
  • Current Time: 10 AM

When customers place orders before 4 PM on weekdays, the custom message will be shown that they will receive their deliveries on the next day. The message will be displayed as “Order by 4 PM to get delivered by tomorrow on 31st January.”

Scenario 2: Order placed on Saturday/Sunday

When orders are placed on weekends, they will be scheduled for delivery on the upcoming Tuesday. The output message explicitly states to ‘order by 4 PM to get delivery on the next Tuesday.’

Solution: Adjust Delivery Dates based on Public Holidays in WooCommerce

While the above feature is super efficient in providing customers with clear and accurate delivery information, store owners might need to skip the delivery dates when they fall on holidays. Here comes a simple code snippet that will let you adjust the delivery date to the next working day when the calculated delivery date falls on a public holiday. For example, if the delivery date is Christmas Day (December 25th), the message will adjust the delivery date to December 26th and show: Order by 4 PM tomorrow for delivery on December 26th.

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');

    // Define an array of public holidays (in Y-m-d format)
    $public_holidays = array(
        '2024-08-19', // // Example Public Holiday
        '2024-12-25', // Christmas Day

        '2024-01-01', // New Year Day
        // Add more public holidays here
    );

    // Initialize variables for delivery date and order by day
    $del_day = '';
    $order_by = '';

    // If FRI/SAT/SUN delivery will be MON
    if ($current_day >= 5) {
        $del_day = date("Y-m-d", strtotime("next monday"));
        $order_by = "Monday";
    }
    // If MON/THU after 4 PM delivery will be TOMORROW
    elseif ($current_day >= 2 && $current_day <= 4 && $current_hour >= 16) {
        $del_day = date("Y-m-d", strtotime("tomorrow"));
        $order_by = "tomorrow";
    }
    // If MON/THU before 4 PM delivery will be TODAY
    else {
        $del_day = date("Y-m-d", strtotime("today"));
        $order_by = "today";
    }

    // Check if the delivery date falls on a public holiday
    while (in_array($del_day, $public_holidays)) {
        $del_day = date("Y-m-d", strtotime($del_day . ' +1 day'));
    }

    // Format the delivery day for display
    $formatted_del_day = date("l jS F", strtotime($del_day));

    // Output the delivery message
    $html = "<br><div class='woocommerce-message' style='clear:both'>Order by 4 PM {$order_by} for delivery on {$formatted_del_day}</div>";
    echo $html;
}

Code Output

Case 1:

A customer who is interested in purchasing a 32-inch LED Television visits the electronics retailer’s product page on 16th August (Wednesday) at 10 AM. The code will check the current day and hour. If the order is placed before 4 PM, the code displays: “Order by 4 PM today for delivery on Wednesday 16th August.”

Case 2:

Another customer who is also interested in buying the same 32-inch LED TV visits the electronics retailer’s product page on 16th August at 5 PM. The code checks the current day (Wednesday) and hour (5 PM) which is above the specified hour in the code. Thus the code displays the message: “Order by 4 PM tomorrow for delivery on Thursday 17th August.”

Case 3:

A different customer explores the electronics retailer’s product page and orders the product on 19th August (Saturday) at 3 PM. Here, the customer is trying to order on the 6th day of the week(Saturday), which is above the specified day in the code. Thus it displays the message “Order by 4 PM Monday for delivery on Monday 21st August.”

Рейтинг
( Пока оценок нет )
Понравилась статья? Поделиться с друзьями:
Добавить комментарий

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!:
Нажимая на кнопку "Отправить комментарий", я даю согласие на обработку персональных данных и принимаю политику конфиденциальности.