Display Estimated Delivery Date

Displays an estimated delivery date based on the day of the order for WooCommerce products.

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

function cb_display_estimated_delivery() {
    date_default_timezone_set( 'Europe/Rome' );

    // Get the current day of the week (1 = Monday, 7 = Sunday)
    $day_of_week = date( 'N' );

    // Calculate the estimated delivery day based on the order day
    switch ( $day_of_week ) {
        case 1: // Monday
            $delivery_day = date_i18n( 'l j F', strtotime( '+2 days' ) );
            break;
        case 2: // Tuesday
            $delivery_day = date_i18n( 'l j F', strtotime( '+2 days' ) );
            break;
        case 3: // Wednesday
            $delivery_day = date_i18n( 'l j F', strtotime( '+2 days' ) );
            break;
        case 4: // Thursday
            $delivery_day = date_i18n( 'l j F', strtotime( '+4 days' ) );
            break;
        case 5: // Friday
            $delivery_day = date_i18n( 'l j F', strtotime( '+4 days' ) );
            break;
        case 6: // Saturday
            $delivery_day = date_i18n( 'l j F', strtotime( '+3 days' ) );
            break;
        case 7: // Sunday
            $delivery_day = date_i18n( 'l j F', strtotime( '+2 days' ) );
            break;
        default:
            $delivery_day = date_i18n( 'l j F', strtotime( '+3 days' ) ); // Default case for safety
    }

    // Convert delivery day text to lowercase
    $delivery_day = strtolower( $delivery_day );

    // Display the estimated delivery information on the WooCommerce product page
    echo '<div class="estimate-delivery">';
    echo '<i class="fa-solid fa-truck-fast"></i>';
    echo '<span> Estimated delivery by </span>';
    echo '<span><strong>' . esc_html( $delivery_day ) . '</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