Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 在WooCommerce中初始化jQueryUIDatePicker插件_Wordpress_Woocommerce_Datepicker - Fatal编程技术网

Wordpress 在WooCommerce中初始化jQueryUIDatePicker插件

Wordpress 在WooCommerce中初始化jQueryUIDatePicker插件,wordpress,woocommerce,datepicker,Wordpress,Woocommerce,Datepicker,要选择日期范围,我使用jQueryUIDatePicker插件。我使用一个代码来显示选择期间的天数 注册日期选择器jQuery插件: add_action('wp_enqueue_scripts', 'enabling_date_picker'); function enabling_date_picker() { // Only on front-end and product page if (is_product() && !is_wc_endpoint

要选择日期范围,我使用jQueryUIDatePicker插件。我使用一个代码来显示选择期间的天数

注册日期选择器jQuery插件:

add_action('wp_enqueue_scripts', 'enabling_date_picker');

function enabling_date_picker() {

    // Only on front-end and product page
    if (is_product() && !is_wc_endpoint_url()):

    // Load the Datepicker jQuery-ui plugin script
    wp_enqueue_style('jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css');
    wp_enqueue_script('jquery-ui-datepicker');

    endif;
}
插件初始化:

// The jQuery script
add_action('wp_footer', 'rental_date_jquery_script');

function rental_date_jquery_script() {
    // Only on front-end and product page
    if (is_product() && !is_wc_endpoint_url()):

            ?>

            <script>
            var from = new Date();
    var to = new Date();
    var dayDiff = 1;

    $(function() {
            var dates = $("#from, #to").datepicker({
                    defaultDate: "+1w",
                    changeMonth: true,
                    numberOfMonths: 1,
                    dateFormat: "dd.mm.yy",
                    minDate: 0,
                    maxDate: 14,
                    onSelect: function(selectedDate) {
                            var option = this.id == "from" ? "minDate" : "maxDate",
                                    instance = $(this).data("datepicker"),
                                    date = $.datepicker.parseDate(
                                            instance.settings.dateFormat ||
                                            $.datepicker._defaults.dateFormat,
                                            selectedDate, instance.settings);
                            dates.not(this).datepicker("option", option, date);

                            if (this.id == "from") {
                                    from = $(this).datepicker('getDate');
                                    if (!(to == "")) {
                                            update_days()
                                    }
                            }
                            if (this.id == "to") {
                                    to = $(this).datepicker('getDate');
                                    update_days()
                            }
                    }
            });
    });       

    function update_days() {
            dayDiff = Math.ceil((to - from) / (1000 * 60 * 60 * 24));
            $("#days").empty()
            $("#days").append(dayDiff)
    } 
</script> 

<?php

    endif;
}
但由于某些原因,日期选择器不工作,日历窗体处于非活动状态

// Add a custom field before single add to cart
add_action('woocommerce_before_variations_form', 'display_rental_date_custom_fields', 5);

function display_rental_date_custom_fields() {

    echo '<div>
            <h3>From:</h3>
            <input id="from" type="text" name="from" readonly />
        </div>
        <div>
            <h3>To:</h3>
            <input id="to" type="text" name="to" readonly />
        </div>
        <div>
            <span>You have chosen: </span>
            <span id="days">< /span> days.
        </div>';
}

如何修复我的代码以使日期选择器正常工作?

因为您使用了jquery之外的函数。请试试这个

            <script>
   jQuery(function($) {
        var from = new Date();
        var to = new Date();
        var dayDiff = 1;
         var dates = $("#from, #to").datepicker({
                defaultDate: "+1w",
                changeMonth: true,
                numberOfMonths: 1,
                dateFormat: "dd.mm.yy",
                minDate: 0,
                maxDate: 14,
                onSelect: function(selectedDate) {
                        var option = this.id == "from" ? "minDate" : "maxDate",
                                instance = $(this).data("datepicker"),
                                date = $.datepicker.parseDate(
                                        instance.settings.dateFormat ||
                                        $.datepicker._defaults.dateFormat,
                                        selectedDate, instance.settings);
                        dates.not(this).datepicker("option", option, date);

                        if (this.id == "from") {
                                from = $(this).datepicker('getDate');
                                if (!(to == "")) {
                                        update_days()
                                }
                        }
                        if (this.id == "to") {
                                to = $(this).datepicker('getDate');
                                update_days()
                        }
                }
        });
        function update_days() {
                dayDiff = Math.ceil((to - from) / (1000 * 60 * 60 * 24));
                $("#days").empty()
                $("#days").append(dayDiff)
        } 
    });       

</script>