การจัดรูปแบบวันที่โดยใช้ date_i18n สำหรับ WooCommerce Delivery Message
การใช้ WooCommerce ฉันได้เชื่อมต่อกับที่เก็บถาวรหน้าผลิตภัณฑ์รถเข็นและการชำระเงินโดยใช้ตะขอต่อไปนี้:
woocommerce_before_single_product_summary
woocommerce_before_shop_loop
woocommerce_before_cart
woocommerce_before_checkout_form
จากนั้นฉันก็ใช้if
อาร์กิวเมนต์ตามด้วยelseif
และสุดท้าย, else
.
อาร์กิวเมนต์เหล่านี้ควบคุมข้อความการส่งมอบที่แสดงต่อลูกค้า / ผู้เยี่ยมชม
ปัญหาหลักของฉันคือการจัดรูปแบบวันที่
เอาท์พุทปัจจุบัน:
คำสั่งซื้อที่ทำก่อน 18.00 น. วันจันทร์จะจัดส่งในวันอังคารที่ 18 สิงหาคมหรือวันถัดไปอย่างช้าที่สุด
ผลลัพธ์ที่คาดหวัง:
คำสั่งซื้อที่ทำก่อน 18.00 น. วันจันทร์จะจัดส่งในวันอังคารที่ 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;
}
คำตอบ
การใช้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 ของธีมลูกที่ใช้งานอยู่ (หรือธีมที่ใช้งานอยู่) ผ่านการทดสอบและใช้งานได้จริง