Display Estimated Delivery Date Range

Displays an estimated delivery date range on the WooCommerce product page based on the day of order.

PHP
/**
 * Snippet Name:     Display Estimated Delivery Date
 * Snippet Author:   coding-bunny.com
 * Description:      Displays an estimated delivery date range on the WooCommerce product page based on the day of order.
 */

add_action( 'woocommerce_after_add_to_cart_button', 'cb_estimated_delivery' );

/**
 * Displays estimated delivery date based on current day of the week.
 */
function cb_estimated_delivery() {
    // Set timezone to Rome
    date_default_timezone_set( 'Europe/Rome' );
    
    // Get today's date and the day of the week (1 = Monday, 7 = Sunday)
    $current_date = strtotime('today');
    $day_of_week = date('N', $current_date);
    
    // Calculate estimated delivery range based on the day of the week
    switch ($day_of_week) {
        case 1: // Monday
            $delivery_min = strtotime('+2 days', $current_date);
            $delivery_max = strtotime('+3 days', $current_date);
            break;
        case 2: // Tuesday
            $delivery_min = strtotime('+2 days', $current_date);
            $delivery_max = strtotime('+3 days', $current_date);
            break;
        case 3: // Wednesday
            $delivery_min = strtotime('+2 days', $current_date);
            $delivery_max = strtotime('+5 days', $current_date);
            break;
        case 4: // Thursday
            $delivery_min = strtotime('+4 days', $current_date);
            $delivery_max = strtotime('+5 days', $current_date);
            break;
        case 5: // Friday
            $delivery_min = strtotime('+4 days', $current_date);
            $delivery_max = strtotime('+5 days', $current_date);
            break;
        case 6: // Saturday
            $delivery_min = strtotime('+3 days', $current_date);
            $delivery_max = strtotime('+4 days', $current_date);
            break;
        case 7: // Sunday
            $delivery_min = strtotime('+2 days', $current_date);
            $delivery_max = strtotime('+3 days', $current_date);
            break;
    }
    
    // Format dates in 'day name day month' format and convert to lowercase
    $delivery_min_formatted = strtolower(date_i18n( 'l j M', $delivery_min ));
    $delivery_max_formatted = strtolower(date_i18n( 'l j M', $delivery_max ));
    
    // Output estimated delivery date range
    echo '<div class="estimate-delivery">';
    echo '<i class="fa-solid fa-truck-fast"></i>';
    echo '<span> Receive it between </span>';
    echo '<span><strong>' . esc_html($delivery_min_formatted) . '</strong></span>';
    echo '<span> and </span>';
    echo '<span><strong>' . esc_html($delivery_max_formatted) . '</strong></span>';
    echo '</div>';
}

Use this snippet to activate the FontAwesome icons: Enqueue Font Awesome icons

How To Implement This Solution?

Leave a Reply

Your email address will not be published. Required fields are marked *

My Agile Privacy
This site uses technical and profiling cookies. You can accept, decline or customize cookies by pressing the desired buttons. By closing this policy you will continue without accepting.

Need help?

Choose one of the following options:

Powered by CodingBunny