
在电子商务中,清晰明确的送达时间预估对于提升用户信任和转化率至关重要。默认的woocommerce设置可能无法满足所有复杂的业务需求,例如根据特定产品属性(如“现货”)或库存状态来动态调整送达提示。本教程将深入探讨如何通过代码定制,在woocommerce产品详情页上实现这一高级功能,确保只有符合条件的产品才显示定制化的送达预估,并且信息精准、专业。
为了实现更智能的送达预估功能,我们将解决以下三个主要需求:
要实现上述功能,我们将主要利用WooCommerce和WordPress提供的以下核心功能:
我们将逐步构建一个PHP函数,并将其挂载到WooCommerce的相应钩子上。
首先,我们需要获取当前产品对象,并检查它是否属于我们指定的自定义分类。
// 定义自定义分类法和术语的别名(slug),便于维护
define( 'CUSTOM_DELIVERY_TAXONOMY', 'product_status' ); // 替换为你的自定义分类法别名
define( 'AVAILABLE_NOW_TERM_SLUG', 'available-now' ); // 替换为你的“现货”分类术语别名
add_action( 'woocommerce_before_add_to_cart_form', 'custom_delivery_estimate' );
function custom_delivery_estimate() {
global $product;
// 确保当前是产品页面,并且产品对象有效
if ( ! is_product() || ! $product ) {
return;
}
// 检查产品是否具有指定的自定义分类
$terms = wp_get_post_terms( $product->get_id(), CUSTOM_DELIVERY_TAXONOMY );
// 错误处理或如果未找到任何术语,则退出
if ( is_wp_error( $terms ) || empty( $terms ) ) {
return;
}
$is_available_now = false;
foreach ( $terms as $term ) {
if ( $term->slug === AVAILABLE_NOW_TERM_SLUG ) {
$is_available_now = true;
break;
}
}
// 如果产品不属于“现货”分类,则不显示预估
if ( ! $is_available_now ) {
return;
}
// 后续逻辑将在此处添加
}在显示送达预估之前,必须确保产品有库存。如果产品缺货,则不应显示任何送达信息。
// ... (上一步的代码)
// 如果产品不属于“现货”分类,则不显示预估
if ( ! $is_available_now ) {
return;
}
// 检查产品库存状态,如果缺货则不显示
if ( ! $product->is_in_stock() ) {
return;
}
// 后续逻辑将在此处添加这是最复杂的部分,我们需要根据当前的日期和时间来计算送达日期范围,并确定用户需要在何时下单才能享受此预估。
// ... (上一步的代码)
// 设置时区,确保日期计算准确
date_default_timezone_set( 'Europe/Tallinn' ); // 根据您的实际位置调整时区
$current_hour = (int) date( 'H' );
$current_minute = (int) date( 'i' );
$cutoff_hour = 16; // 下单截止时间为16:00 (4 PM)
$order_by_message = '';
$delivery_start_timestamp = 0;
$delivery_end_timestamp = 0;
// 计算距离下单截止时间剩余的小时和分钟
if ( $current_hour < $cutoff_hour ) {
// 今天16:00前
$hours_left = $cutoff_hour - $current_hour;
$minutes_left = 0;
if ( $current_minute > 0 ) {
$hours_left--;
$minutes_left = 60 - $current_minute;
}
$order_by_message = "今日16:00前下单,还剩 " . ($hours_left > 0 ? $hours_left . " 小时 " : "") . ($minutes_left > 0 ? $minutes_left . " 分钟" : "") . " 下单";
if ( $hours_left <= 0 && $minutes_left <= 0 ) { // 如果时间已非常接近或已到
$order_by_message = "今日16:00前下单";
}
} else {
// 已过今天16:00,截止时间是明天16:00
$hours_until_tomorrow_cutoff = (24 - $current_hour) + $cutoff_hour;
$minutes_until_tomorrow_cutoff = (60 - $current_minute) % 60;
if ( $minutes_until_tomorrow_cutoff > 0 ) {
$hours_until_tomorrow_cutoff--;
}
$order_by_message = "明日16:00前下单,还剩 " . ($hours_until_tomorrow_cutoff > 0 ? $hours_until_tomorrow_cutoff . " 小时 " : "") . ($minutes_until_tomorrow_cutoff > 0 ? $minutes_until_tomorrow_cutoff . " 分钟" : "") . " 下单";
if ( $hours_until_tomorrow_cutoff <= 0 && $minutes_until_tomorrow_cutoff <= 0 ) {
$order_by_message = "明日16:00前下单";
}
}
// 根据当前日期和时间确定送达日期范围
$current_day_of_week = date( 'N' ); // 1 (周一) - 7 (周日)
if ( $current_day_of_week >= 5 ) { // 周五 (5), 周六 (6), 周日 (7)
// 周末下单,最早下周二送达
$delivery_start_timestamp = strtotime( "next tuesday" );
} elseif ( $current_hour >= $cutoff_hour ) { // 周一至周四,已过16:00
// 今天16:00后下单,最早后天送达
$delivery_start_timestamp = strtotime( "tomorrow + 1 day" );
} else { // 周一至周四,16:00前
// 今天16:00前下单,最早明天送达
$delivery_start_timestamp = strtotime( "tomorrow" );
}
// 定义一个2天的送达窗口
$delivery_end_timestamp = strtotime( "+1 day", $delivery_start_timestamp );
$delivery_start_date_formatted = date( "n月j日", $delivery_start_timestamp );
$delivery_end_date_formatted = date( "n月j日", $delivery_end_timestamp );
// 后续逻辑将在此处添加最后,将所有计算出的信息整合到一个HTML字符串中,并使用WooCommerce的样式类进行显示。
// ... (上一步的代码)
$html = "<br><div class='woocommerce-message' style='clear:both; padding: 1em; border-left: 5px solid #8cc63f; background-color: #e6f7d9; margin-bottom: 1em;'>" .
"<strong>预计送达日期:</strong>{$delivery_start_date_formatted} 至 {$delivery_end_date_formatted}。" .
" ({$order_by_message})" .
"</div>";
echo $html;
}将以上所有步骤整合到您的主题 functions.php 文件或自定义插件中。
<?php // 定义自定义分类法和术语的别名(slug),便于维护 // 请根据您的实际设置进行修改 define( 'CUSTOM_DELIVERY_TAXONOMY', 'product_status' ); // 您的自定义分类法别名 define( 'AVAILABLE_NOW_TERM_SLUG', 'available-now' ); // 您的“现货”分类术语别名 add_action( 'woocommerce_before_add_to_cart_form', 'custom_delivery_estimate' );
以上就是WooCommerce产品页面:基于自定义分类和库存状态显示动态预计送达日期的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号