【问题标题】:Formatting Date using date_i18n for WooCommerce Delivery Message为 WooCommerce 传递消息使用 date_i18n 格式化日期
【发布时间】:2020-08-16 06:01:00
【问题描述】:

使用 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 天。

我已经阅读了“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;
}

【问题讨论】:

    标签: php wordpress date woocommerce date-formatting


    【解决方案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() {
        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 文件中。经过测试并且可以工作。

    【讨论】:

    • 啊,现在我明白了它背后的格式了。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 2016-08-21
    相关资源
    最近更新 更多