亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 date_i18n 格式化 WooCommerce 傳遞消息的日期

使用 date_i18n 格式化 WooCommerce 傳遞消息的日期

PHP
子衿沉夜 2023-10-15 14:52:26
使用 WooCommerce,我使用以下掛鉤連接到存檔、產品頁面、購物車和結賬:woocommerce_before_single_product_summarywoocommerce_before_shop_loopwoocommerce_before_cartwoocommerce_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;}
查看完整描述

1 回答

?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

使用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 文件中。經過測試并有效。


查看完整回答
反對 回復 2023-10-15
  • 1 回答
  • 0 關注
  • 175 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號