Formato de fecha usando date_i18n para el mensaje de entrega de WooCommerce

Aug 16 2020

Con WooCommerce, me conecté al archivo, la página del producto, el carrito y el pago con los siguientes enlaces:

  • woocommerce_before_single_product_summary
  • woocommerce_before_shop_loop
  • woocommerce_before_cart
  • woocommerce_before_checkout_form

Entonces estoy usando un ifargumento seguido de un elseify, por último, else.

Estos argumentos controlan el mensaje de entrega que se muestra al cliente/visitante.

Mi principal problema es formatear la fecha.

Salida de corriente:

Los pedidos realizados antes de las 18:00 horas del lunes se entregarán el martes 18 de agosto o al día siguiente a más tardar.

Rendimiento esperado:

Los pedidos realizados antes de las 18:00 horas del lunes se entregarán el martes 18 de agosto o al día siguiente como máximo.

En otras palabras, quiero agregar "el" y "de" a esa oración, haciéndola más clara y fácil de leer.

Los adicionales "el" y "de" funcionarán con cualquier formato, como 1, 2, 5 o incluso 23 en términos de días del mes.

He leído la página del códice de WordPress " Formato de fecha y hora ", pero incluso cuando uso el formato "\t\h\e" y "\o\f", la visualización de la fecha tiene un formato incorrecto.

Este es mi código hasta ahora. Si alguien puede formatear la fecha por mí o explicar cómo cambiarla en mi resultado deseado, estaría agradecido.

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;
}

Respuestas

LoicTheAztec Aug 16 2020 at 20:07

Usando l, \t\h\e jS \of F,una cadena de formato, obtengo el resultado correcto con su código. También he realizado algunos cambios, como reemplazar "day after tomorrow"y "+2 days"usar sprintf()la función:

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>';
}

El código va en el archivo functions.php de su tema secundario activo (o tema activo). Probado y funciona.