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

WooCommerce(Bizberg主题)ui-datepicker-year仅显示20年,需扩展至99年

WooCommerce日期选择器年份范围扩展问题解决方法

你的代码未生效通常是执行时机不对、选择器不匹配或初始化逻辑冲突导致的,以下是针对性的修复方案:

方案1:延迟执行脚本确保覆盖初始化设置

原代码可能在日期选择器完成初始化前就运行了,导致配置无法生效。添加延迟并适配主题可能的字段选择器:

function extend_date_of_birth_range() {
    // 仅在需要日期选择器的页面加载脚本
    if ( is_checkout() || is_account_page() ) {
    ?>
    <script>
        jQuery(document).on('ready', function($) {
            // 延迟100ms确保日期选择器已初始化
            setTimeout(function() {
                const currentYear = new Date().getFullYear();
                const yearRange = `${currentYear - 99}:${currentYear - 10}`;

                // 同时匹配ID和主题常用类选择器,避免选择器错误
                $('#billing_date_of_birth, .bizberg-datepicker, .woocommerce-datepicker').each(function() {
                    $(this).datepicker('option', 'yearRange', yearRange);
                    // 强制开启年份切换功能(部分主题默认关闭)
                    $(this).datepicker('option', 'changeYear', true);
                });
            }, 100);
        });
    </script>
    <?php
    }
}
add_action('wp_footer', 'extend_date_of_birth_range');

方案2:直接覆盖日期选择器初始化参数

如果主题是通过自定义脚本初始化日期选择器,用wp_add_inline_script强制覆盖配置:

function override_datepicker_defaults() {
    if ( is_checkout() || is_account_page() ) {
        wp_enqueue_script('jquery-ui-datepicker');
        
        $inline_script = "
        jQuery(document).on('ready', function($) {
            const currentYear = new Date().getFullYear();
            $('.datepicker, #billing_date_of_birth').datepicker({
                changeYear: true,
                yearRange: `${currentYear - 99}:${currentYear - 10}`
            });
        });
        ";
        wp_add_inline_script('jquery-ui-datepicker', $inline_script);
    }
}
add_action('wp_enqueue_scripts', 'override_datepicker_defaults');

关键检查步骤

  1. 确认字段选择器:打开浏览器开发者工具(F12),找到出生日期输入框,查看它的ID或类名,替换代码中的选择器
  2. 清空缓存:清除WordPress缓存、浏览器缓存,若使用缓存插件(如WP Rocket)需同步清空插件缓存
  3. 验证依赖加载:在页面源码中搜索jquery-ui-datepicker,确认该脚本已正确加载

内容的提问来源于stack exchange,提问作者Richard Fenton

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 10:51:03