使用 date_i18n 格式化 WooCommerce 传递消息的日期

使用 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”适用于任何格式,例如按月份的第 1 日、第 2 日、第 5 日甚至第 23 日。

我已阅读“格式化日期和时间”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;

}


子衿沉夜
浏览 115回答 1
1回答

汪汪一只猫

使用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() {&nbsp; &nbsp; date_default_timezone_set( 'Europe/Paris' );&nbsp; &nbsp; $date_format = __('l, \t\h\e jS \of F,', 'woocommerce');&nbsp; &nbsp; // 1. Delivery cut-off for friday and weekend&nbsp; &nbsp; if ( date_i18n('N') >= 5 ) {&nbsp; &nbsp; &nbsp; &nbsp; $delivery_day = date_i18n( $date_format, strtotime( "next wednesday" ) );&nbsp; &nbsp; &nbsp; &nbsp; $order_before = __('Monday', 'woocommerce');&nbsp; &nbsp; }&nbsp; &nbsp; // 2. From monday to thursday before XX (currently set to 18 = 6PM), delivery will be on next week tuesday&nbsp; &nbsp; elseif ( date_i18n('H') >= 18 ) {&nbsp; &nbsp; &nbsp; &nbsp; $delivery_day = date_i18n( $date_format, strtotime( "+2 days" ) );&nbsp; &nbsp; &nbsp; &nbsp; $order_before = __('tomorrow', 'woocommerce');&nbsp; &nbsp; }&nbsp; &nbsp; // 3. From monday to thursday within the cut-off time, delivery will be next day (tomorrow)&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; $delivery_day = date_i18n( $date_format, strtotime( "+1 day" ) );&nbsp; &nbsp; &nbsp; &nbsp; $order_before = __('today', 'woocommerce');&nbsp; &nbsp; }&nbsp; &nbsp; $message = sprintf(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; __("Orders made before 6PM %s will be delivered on %s or the day after at the latest.", "woocommerce"),&nbsp; &nbsp; &nbsp; &nbsp; $order_before, $delivery_day&nbsp;&nbsp; &nbsp; );&nbsp; &nbsp; echo '<div class="product-delivery-message" style="clear:both">' . $message . '</div>';}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
打开App,查看更多内容
随时随地看视频慕课网APP