Formatting Date using date_i18n for WooCommerce Delivery Message

Aug 16 2020

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

  • woocommerce_before_single_product_summary
  • woocommerce_before_shop_loop
  • woocommerce_before_cart
  • woocommerce_before_checkout_form

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.

Ek "the" ve "of", ayın günleri açısından 1., 2., 5. ve hatta 23. gibi herhangi bir biçimlendirmeyle çalışacaktır.

" Tarih ve Saati Biçimlendirme " WordPress Codex sayfasını okudum , ancak "\ t \ h \ e" ve "\ o \ f" biçimlendirmesini kullanırken bile tarih ekranı yanlış biçimlendirilmiş.

Şimdiye kadarki kodum bu. Biri benim için tarihi biçimlendirebilir veya istediğim çıktıya nasıl değiştirebileceğini açıklayabilirse, minnettar olurum.

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

Yanıtlar

LoicTheAztec Aug 16 2020 at 20:07

l, \t\h\e jS \of F,Biçimlendirme dizesini kullanarak , kodunuzla doğru çıktıyı alıyorum. Ben de değiştirilmiştir gibi bazı değişiklikler yapmış "day after tomorrow"olan "+2 days"ve kullanılan sprintf()fonksiyonu:

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

Kod, aktif alt temanızın (veya aktif temanızın) functions.php dosyasına gider. Test edildi ve çalışıyor.