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:
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.
Дополнительные «the» и «of» будут работать с любым форматированием, таким как 1-е, 2-е, 5-е или даже 23-е с точки зрения дней месяца.
Я прочитал страницу Кодекса WordPress « Форматирование даты и времени », но даже при использовании форматирования «\ t \ h \ e» и «\ o \ f» отображение даты отображается неправильно.
Пока это мой код. Если кто-нибудь может отформатировать дату для меня или объяснить, как изменить ее на желаемый результат, я был бы признателен.
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;
}
Ответы
Используя l, \t\h\e jS \of F,
строку форматирования, я получаю правильный результат с вашим кодом. Я также сделал некоторые изменения , как на смену "day after tomorrow"
с "+2 days"
и используются sprintf()
функции:
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>';
}
Код находится в файле functions.php вашей активной дочерней темы (или активной темы). Проверено и работает.