WooCommerce自定义分类法下商品展示预计配送时间代码修改咨询
以下是完全适配你需求的修改后代码:
add_action( 'woocommerce_before_add_to_cart_form', 'delivery_estimate' ); function delivery_estimate() { global $product; // 非商品对象/缺货/不属于Available now分类直接返回 if ( ! is_a( $product, 'WC_Product' ) || ! $product->is_in_stock() || ! has_term( 'available-now', '替换为你的自定义分类法Slug', $product->get_id() ) ) { return; } date_default_timezone_set( 'Europe/Tallinn' ); $now = time(); $today_16 = strtotime( 'today 16:00' ); $deadline_ts = 0; $min_delivery_ts = 0; // 计算下单截止时间和最早配送时间 if ( date( 'N' ) >= 5 ) { // 周五及以后 $deadline_ts = strtotime( 'next monday 16:00' ); $min_delivery_ts = strtotime( 'next tuesday' ); } elseif ( $now >= $today_16 ) { // 工作日16点后 $deadline_ts = strtotime( 'tomorrow 16:00' ); $min_delivery_ts = strtotime( 'tomorrow +1 day' ); } else { // 工作日16点前 $deadline_ts = $today_16; $min_delivery_ts = strtotime( 'tomorrow' ); } // 计算截止下单剩余时长 $diff = $deadline_ts - $now; $hours = floor( $diff / 3600 ); $mins = floor( ( $diff % 3600 ) / 60 ); $countdown = "{$hours}小时{$mins}分钟"; // 计算配送区间 $delivery_start = date( 'n月j日', $min_delivery_ts ); $delivery_end = date( 'n月j日', $min_delivery_ts + 86400 * 3 ); // 3为配送区间跨度天数,可自行调整 // 输出提示 echo "<br><div class='woocommerce-message' style='clear:both'><strong>若您在{$countdown}内下单,预计可在{$delivery_start}至{$delivery_end}之间完成配送。</strong></div>"; }
注意事项
- 代码中
available-now为Available now分类的默认slug,替换为你的自定义分类法Slug请修改为你后台该自定义分类法的实际标识 - 时区保持原代码的欧洲塔林时区,不需要调整可直接使用
内容的提问来源于stack exchange,提问作者Emmanuel Kuebu
相关产品推荐
相关产品推荐

