You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在WooCommerce商品循环中仅为蛋糕品类按时间规则显示配送日期

在WooCommerce商品循环中为蛋糕品类添加配送时间提示

以下是实现需求的具体代码,逻辑为仅针对蛋糕品类(排除相框品类)展示配送时间,基于Asia/Kolkata时区判断当日/次日配送:

add_action( 'woocommerce_after_shop_loop_item_title', 'display_cake_delivery_time', 15 );
function display_cake_delivery_time() {
    global $product;
    
    // 替换为你的蛋糕品类slug和相框品类slug
    $cake_category_slug = 'cakes';
    $frame_category_slug = 'photo-frames';
    
    // 检查当前商品是否属于蛋糕品类且不属于相框品类
    if ( has_term( $cake_category_slug, 'product_cat', $product->get_id() ) && !has_term( $frame_category_slug, 'product_cat', $product->get_id() ) ) {
        
        // 设置时区为Asia/Kolkata
        date_default_timezone_set( 'Asia/Kolkata' );
        
        $current_hour = (int) date( 'H' );
        $delivery_date = '';
        
        // 判断当前时间是否超过下午6点
        if ( $current_hour >= 18 ) {
            // 次日配送,格式可自定义
            $delivery_date = date( 'Y年m月d日', strtotime( '+1 day' ) );
            echo '<p class="delivery-time">下单时间已过18点,配送日期:' . $delivery_date . '</p>';
        } else {
            // 当日配送
            $delivery_date = date( 'Y年m月d日' );
            echo '<p class="delivery-time">18点前下单,当日配送:' . $delivery_date . '</p>';
        }
    }
}

注意事项:

  • 请将代码中的$cake_category_slug和$frame_category_slug替换为你实际使用的品类slug(可在WooCommerce品类编辑页面查看)
  • 配送日期的格式可通过修改date()函数的参数调整,比如改为'd/m/Y'显示日/月/年
  • 可以通过自定义CSS调整.delivery-time类的样式,让提示更贴合你的主题风格
  • 代码建议添加到当前主题的functions.php文件末尾,或者使用自定义功能插件添加,避免主题更新时丢失修改

内容的提问来源于stack exchange,提问作者artsify in

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 09:48:16