Banner

Display Estimated Delivery Date

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

Display estimated delivery date
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.
 * Version:          1.1.0
 */
 
if ( ! defined( 'ABSPATH' ) ) {
    return;
}

add_action( 'woocommerce_after_add_to_cart_button', 'cbedd_display_estimated_delivery' );

function cbedd_display_estimated_delivery() {
    $old_tz = date_default_timezone_get();
    date_default_timezone_set( 'Europe/Rome' );

    $day_of_week = date( 'N' );

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

    date_default_timezone_set( $old_tz );

    $delivery_day = strtolower( $delivery_day );

    $dashicon = '<span class="cbedd-icon dashicons dashicons-calendar-alt" style="font-size:22px;"></span>';

    $output = sprintf(
        '<div class="cbedd-estimate-delivery" style="padding:16px; border:1px solid #e0e0e0; border-radius:6px; background:#f9f9f9; display:flex; align-items:center; flex-wrap:wrap; gap:12px; margin-top:16px;">
            %1$s
            <span>Estimated delivery by</span>
            <span style="font-weight:700;">%2$s</span>
        </div>',
        $dashicon,
        esc_html( $delivery_day )
    );
    echo wp_kses(
        $output,
        array(
            'div'    => array( 'class' => true, 'style' => true ),
            'span'   => array( 'class' => true, 'style' => true, 'aria-hidden' => true ),
        )
    );
}

add_action( 'wp_enqueue_scripts', function () {
    wp_enqueue_style( 'dashicons' );
});

How To Implement This Solution?

Leave a Reply

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