Data de formatação usando date_i18n para mensagem de entrega WooCommerce
Usando o WooCommerce, conectei-me ao arquivo, página do produto, carrinho e checkout usando os seguintes ganchos:
woocommerce_before_single_product_summary
woocommerce_before_shop_loop
woocommerce_before_cart
woocommerce_before_checkout_form
Estou então usando um if
argumento seguido por um elseif
e, por último, else
.
Esses argumentos controlam a mensagem de entrega exibida ao cliente/visitante.
Meu principal problema é formatar a data.
Saída atual:
Os pedidos feitos antes das 18h de segunda-feira serão entregues na terça-feira, 18 de agosto ou no dia seguinte, o mais tardar.
Saída esperada:
Os pedidos feitos antes das 18h de segunda-feira serão entregues na terça-feira, 18 de agosto ou no dia seguinte, o mais tardar.
Em outras palavras, quero adicionar "the" e "of" a essa frase, tornando-a mais clara e fácil de ler.
O "the" e "of" adicionais funcionarão com qualquer formatação como 1º, 2º, 5º ou mesmo 23º em termos de dias do mês.
Eu li a página do WordPress Codex " Formatando data e hora ", mas mesmo ao usar a formatação "\t\h\e" e "\o\f", a exibição da data é formatada incorretamente.
Este é o meu código até agora. Se alguém puder formatar a data para mim ou explicar como alterá-la para a saída desejada, ficaria grato.
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;
}
Respostas
Usando l, \t\h\e jS \of F,
string de formatação, obtenho a saída correta com seu código. Também fiz algumas alterações, como a função substituída "day after tomorrow"
por "+2 days"
e usada 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>';
}
O código vai no arquivo functions.php do seu tema filho ativo (ou tema ativo). Testado e funciona.