
本文旨在帮助WooCommerce开发者根据产品所属的特定分类(taxonomy)来显示预计交货时间,并提供代码示例,同时涵盖了如何根据当前时间动态调整交货日期、自定义显示信息以及在产品缺货时隐藏交货提示的方法。通过学习本文,你将能够灵活地控制WooCommerce产品页面的交货信息展示,提升用户体验。
在WooCommerce中,经常需要根据产品的不同属性来展示不同的信息。例如,针对特定分类的产品,显示预计的交货时间。以下代码展示了如何根据产品所属的自定义分类(taxonomy)来显示交货时间信息。
add_action( 'woocommerce_before_add_to_cart_form', 'delivery_estimate' );
function delivery_estimate() {
global $product;
// 检查产品类型是否为 'product'
if ( 'product' == $product->post_type ) {
// 获取产品的自定义分类 'available_now'
$terms = wp_get_post_terms( $product->id, 'available_now' );
// 错误处理
if ( $terms instanceof WP_Error ) {
// 在这里记录错误信息,或者进行其他处理
error_log("Error getting terms: " . $terms->get_error_message());
} elseif ( ! empty( $terms ) ) {
$term = $terms[ 0 ]; // 假设产品只有一个 'available_now' 分类
// 检查分类的别名 (slug) 是否为 'ready-to-ship'
if ( 'ready-to-ship' == $term->slug ) {
date_default_timezone_set( 'Europe/Tallinn' );
// 如果是周五/周六/周日,则交货时间为下周一
if ( date( 'N' ) >= 5 ) {
$del_day = date( "l jS F", strtotime( "next tuesday" ) );
$order_by = "Monday";
}
// 如果是周一/周四下午4点后,则交货时间为后天
elseif ( date( 'H' ) >= 16 ) {
$del_day = date( "l jS F", strtotime( "tomorrow + 1 day" ) );
$order_by = "tomorrow";
}
// 如果是周一/周四下午4点前,则交货时间为明天
else {
$del_day = date( "l jS F", strtotime( "tomorrow" ) );
$order_by = "today";
}
// 获取剩余时间,假设截止时间是每天的16:00
$now = time();
$end_of_day = strtotime('today 16:00');
$hours_left = round(($end_of_day - $now) / 3600);
// 构建HTML消息
$html = "<br><div class='woocommerce-message' style='clear:both'>Ready for delivery between " . date("jS F", strtotime($del_day)) . " & " . date("jS F", strtotime($del_day . " + 3 days")) . " when you order within {$hours_left} hours.</div>";
echo $html;
} // endif
} // endif
}代码解释:
注意事项:
除了根据产品分类显示交货时间,还需要考虑产品库存状态。如果产品缺货,则不应该显示交货时间信息。以下代码展示了如何在产品缺货时隐藏交货时间提示。
add_action( 'woocommerce_before_add_to_cart_form', 'delivery_estimate' );
function delivery_estimate() {
global $product;
// 检查产品类型是否为 'product'
if ( 'product' == $product->post_type ) {
// 检查产品是否在库存中
if ( $product->is_in_stock() ) {
// 获取产品的自定义分类 'available_now'
$terms = wp_get_post_terms( $product->id, 'available_now' );
// 错误处理
if ( $terms instanceof WP_Error ) {
// 在这里记录错误信息,或者进行其他处理
error_log("Error getting terms: " . $terms->get_error_message());
} elseif ( ! empty( $terms ) ) {
$term = $terms[ 0 ]; // 假设产品只有一个 'available_now' 分类
// 检查分类的别名 (slug) 是否为 'ready-to-ship'
if ( 'ready-to-ship' == $term->slug ) {
date_default_timezone_set( 'Europe/Tallinn' );
// 如果是周五/周六/周日,则交货时间为下周一
if ( date( 'N' ) >= 5 ) {
$del_day = date( "l jS F", strtotime( "next tuesday" ) );
$order_by = "Monday";
}
// 如果是周一/周四下午4点后,则交货时间为后天
elseif ( date( 'H' ) >= 16 ) {
$del_day = date( "l jS F", strtotime( "tomorrow + 1 day" ) );
$order_by = "tomorrow";
}
// 如果是周一/周四下午4点前,则交货时间为明天
else {
$del_day = date( "l jS F", strtotime( "tomorrow" ) );
$order_by = "today";
}
// 获取剩余时间,假设截止时间是每天的16:00
$now = time();
$end_of_day = strtotime('today 16:00');
$hours_left = round(($end_of_day - $now) / 3600);
// 构建HTML消息
$html = "<br><div class='woocommerce-message' style='clear:both'>Ready for delivery between " . date("jS F", strtotime($del_day)) . " & " . date("jS F", strtotime($del_day . " + 3 days")) . " when you order within {$hours_left} hours.</div>";
echo $html;
} // endif
} // endif
} // endif
}
}代码解释:
总结:
通过以上代码示例,你可以在WooCommerce中根据产品分类和库存状态动态显示预计交货时间。这可以帮助你更好地向客户展示产品信息,提高用户体验,并最终促进销售。请务必根据你的实际需求修改代码,并进行充分的测试。
以上就是WooCommerce教程:根据产品分类显示预计交货时间,并处理库存状态的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号