使用 date_i18n 为 WooCommerce 传递消息格式化日期
Formatting Date using date_i18n for WooCommerce Delivery Message
使用 WooCommerce,我使用以下挂钩连接到存档、产品页面、购物车和结帐:
woocommerce_before_single_product_summary
woocommerce_before_shop_loop
woocommerce_before_cart
woocommerce_before_checkout_form
然后我使用 if
参数,然后是 elseif
,最后是 else
.
这些参数控制向客户/访客显示的递送消息。
我的主要问题是格式化日期。
当前输出:
星期一下午 6 点之前下的订单将在 8 月 18 日星期二或最迟次日送达。
预期输出:
星期一下午 6 点之前下的订单将在星期二、8 月 18 日或最迟次日送达。
换句话说,我想在那个句子中加上“the”和“of”,使其更清晰易读。
额外的“the”和“of”将适用于任何格式,例如 1st、2nd、5th 甚至 23rd(就一个月中的几天而言)。
我已阅读“Formatting Date and Time”WordPress Codex 页面,但即使使用“\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 文件。已测试并有效。
使用 WooCommerce,我使用以下挂钩连接到存档、产品页面、购物车和结帐:
woocommerce_before_single_product_summary
woocommerce_before_shop_loop
woocommerce_before_cart
woocommerce_before_checkout_form
然后我使用 if
参数,然后是 elseif
,最后是 else
.
这些参数控制向客户/访客显示的递送消息。
我的主要问题是格式化日期。
当前输出:
星期一下午 6 点之前下的订单将在 8 月 18 日星期二或最迟次日送达。
预期输出:
星期一下午 6 点之前下的订单将在星期二、8 月 18 日或最迟次日送达。
换句话说,我想在那个句子中加上“the”和“of”,使其更清晰易读。
额外的“the”和“of”将适用于任何格式,例如 1st、2nd、5th 甚至 23rd(就一个月中的几天而言)。
我已阅读“Formatting Date and Time”WordPress Codex 页面,但即使使用“\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 文件。已测试并有效。