简体   繁体   中英

How to disable today's date in jQuery DatePicker (Wordpress)

I'm currently using a plugin called Flexible Checkout Fields Pro. I would like to tweak the settings on the date picker. Our company makes deliveries on orders to tourists staying in hotels and cruise ships. The only thing is we rarely make deliveries on the same day that the orders are placed and also not on weekends. I reached out to the developers and was given this guide to look at: http://www.spiceforms.com/blog/how-to-disable-dates-in-jquery-datepicker-a-short-guide/

Here it gave me coding for 'noWeekends':

$(function() {
    $( "#datepicker" ).datepicker({
        beforeShowDay: $.datepicker.noWeekends
    });
});

I have no idea where to insert this code in my WordPress directories, and I still don't have any info on how to disable the current day on the timepicker... Any help would be appreciated.

You could paste the following into your footer and it should work. Otherwise you can remove the <script></script> tags and paste it in an external JS file as long as it is called after the plugin is loaded.

Let me break it down for you:

Read the comments throughout the code

<script>

// First we have to enable the jQuery '$' selector in WP.
(function($) {

/** Days to be disabled as an array */
var disableddates = [];

function DisableSpecificDates(date) {

    // Get the current date values
    var m = date.getMonth(); // Month
    var d = date.getDate(); // Day
    var y = date.getFullYear(); // Year

    // Convert the date in to the mm-dd-yyyy format
    var currentdate = m + '-' + d + '-' + y ;

    // Add the current date to the disableddates array
    disableddates.push(currentdate);

    // We will now check if the date belongs to disableddates array
    for (var i = 0; i < disableddates.length; i++) {

        // Now check if the current date is in disabled dates array.
        if ($.inArray(currentdate, disableddates) != -1 ) {
            return [false];
        }
    }

    // In case the date is not present in disabled array, we will now check if it is a weekend.
    // We will use the noWeekends function
    var weekenddate = $.datepicker.noWeekends(date);
    return weekenddate;

}

// Initialize the datepicker function and call the 'DisableSpecificDates' as the 'beforeShowDay' callback.
$( "#datepicker" ).datepicker({
    beforeShowDay: DisableSpecificDates
});

})(jQuery); // Close out WP jQuery.
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM