user14112966
user14112966

Reputation:

Formatting Date using date_i18n for WooCommerce Delivery Message

Using WooCommerce, I have hooked into archive, product page, cart and checkout using the following hooks:

I am then using an if argument followed by an elseif and lastly, else.

These arguments controls the delivery message displayed to the customer / visitor.

My main problem is formatting the date.

Current output:

Orders made before 6PM Monday will be delivered on Tuesday, August 18th, or the day after at the latest.

Expected output:

Orders made before 6PM Monday will be delivered on Tuesday, the 18th of August, or the day after at the latest.

In other words, I want to add "the" and "of" to that sentence, making it clearer and easier to read.

The additional "the" and "of" will work with any formatting such as 1st, 2nd, 5th or even 23rd in terms of days of the month.

I've read the "Formatting Date and Time" WordPress Codex page, but even when using the "\t\h\e" and "\o\f" formatting, the date display is incorrectly formatted.

This is my code so far. If anyone can format the date for me or explain how to change it into my desired output, I would be grateful.

add_action( 'woocommerce_before_single_product_summary', 'product_delivery_message' );
add_action( 'woocommerce_before_shop_loop', 'product_delivery_message' );
add_action( 'woocommerce_before_cart', 'product_delivery_message' );
add_action( 'woocommerce_before_checkout_form', 'product_delivery_message' );

function product_delivery_message() {

date_default_timezone_set( 'Europe/Paris' );

    // delivery cut-off for friday and weekend

    if ( date_i18n( 'N' ) >= 5 ) {

        $delivery_day = date_i18n( "l, F jS, ", strtotime( "next wednesday" ));

        $order_before = "Monday";
    }

    // on monday to thursday before XX (currently set to 18 = 6PM), delivery will be on next week tuesday

    elseif ( date_i18n( 'H' ) >= 18 ) {

        $delivery_day = date_i18n( "l, F jS, ", strtotime( "day after tomorrow" ));

        $order_before = "tomorrow";
    }

    // monday to thursday within the cut-off time, delivery will be next day (tomorrow)

    else {

        $delivery_day = date_i18n( "l, F jS, ", strtotime( "tomorrow" ));

        $order_before = "today";
    }

    $delivery_message = "<div class='product-delivery-message' style='clear:both'>Orders made before 6PM {$order_before} will be delivered on {$delivery_day} or the day after at the latest.</div>";
    
    echo $delivery_message;
}

Upvotes: 2

Views: 572

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254353

Using l, \t\h\e jS \of F, formatting string, I get the correct output with your code. I have also made some changes like replaced "day after tomorrow" with "+2 days" and used sprintf() function:

add_action( 'woocommerce_before_single_product_summary', 'product_delivery_message' );
add_action( 'woocommerce_before_shop_loop', 'product_delivery_message' );
add_action( 'woocommerce_before_cart', 'product_delivery_message' );
add_action( 'woocommerce_before_checkout_form', 'product_delivery_message' );

function product_delivery_message() {
    date_default_timezone_set( 'Europe/Paris' );

    $date_format = __('l, \t\h\e jS \of F,', 'woocommerce');

    // 1. Delivery cut-off for friday and weekend
    if ( date_i18n('N') >= 5 ) {
        $delivery_day = date_i18n( $date_format, strtotime( "next wednesday" ) );
        $order_before = __('Monday', 'woocommerce');
    }
    // 2. From monday to thursday before XX (currently set to 18 = 6PM), delivery will be on next week tuesday
    elseif ( date_i18n('H') >= 18 ) {
        $delivery_day = date_i18n( $date_format, strtotime( "+2 days" ) );
        $order_before = __('tomorrow', 'woocommerce');
    }
    // 3. From monday to thursday within the cut-off time, delivery will be next day (tomorrow)
    else {
        $delivery_day = date_i18n( $date_format, strtotime( "+1 day" ) );
        $order_before = __('today', 'woocommerce');
    }

    $message = sprintf( 
        __("Orders made before 6PM %s will be delivered on %s or the day after at the latest.", "woocommerce"),
        $order_before, $delivery_day 
    );

    echo '<div class="product-delivery-message" style="clear:both">' . $message . '</div>';
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Upvotes: 2

Related Questions