Formattazione della data utilizzando date_i18n per il messaggio di consegna di WooCommerce
Utilizzando WooCommerce, mi sono collegato all'archivio, alla pagina del prodotto, al carrello e al checkout utilizzando i seguenti hook:
woocommerce_before_single_product_summary
woocommerce_before_shop_loop
woocommerce_before_cart
woocommerce_before_checkout_form
Sto quindi usando un if
argomento seguito da un elseif
e infine, else
.
Questi argomenti controllano il messaggio di consegna visualizzato al cliente/visitatore.
Il mio problema principale è la formattazione della data.
Uscita in corrente:
Gli ordini effettuati prima delle 18:00 di lunedì verranno consegnati martedì 18 agosto o al più tardi il giorno successivo.
Uscita prevista:
Gli ordini effettuati prima delle 18:00 di lunedì verranno consegnati martedì 18 agosto o al più tardi il giorno successivo.
In altre parole, voglio aggiungere "the" e "of" a quella frase, rendendola più chiara e facile da leggere.
L'ulteriore "the" e "of" funzioneranno con qualsiasi formattazione come 1st, 2nd, 5th o anche 23rd in termini di giorni del mese.
Ho letto la pagina " Formattazione di data e ora " del codice WordPress, ma anche quando si utilizza la formattazione "\t\h\e" e "\o\f", la visualizzazione della data è formattata in modo errato.
Questo è il mio codice finora. Se qualcuno può formattare la data per me o spiegare come cambiarla nell'output desiderato, sarei 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;
}
Risposte
Usando l, \t\h\e jS \of F,
la stringa di formattazione, ottengo l'output corretto con il tuo codice. Ho anche apportato alcune modifiche come sostituito "day after tomorrow"
con "+2 days"
e sprintf()
funzione usata:
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>';
}
Il codice va nel file functions.php del tuo tema figlio attivo (o tema attivo). Testato e funziona.