Skip to content

Instantly share code, notes, and snippets.

@Garconis
Last active January 6, 2020 20:29
Show Gist options
  • Save Garconis/8e92ab28448c6e1d9e20674ee579a068 to your computer and use it in GitHub Desktop.
Save Garconis/8e92ab28448c6e1d9e20674ee579a068 to your computer and use it in GitHub Desktop.
WooCommerce | Checkout Field Editor | Adjust datepicker to have custom date format, certain future range, and blocked dates
<?php
function fs_custom_adjust_datepicker_range () {
// only adjust on Checkout page
if ( is_checkout() ) {
wp_enqueue_script( 'jquery' );
?>
<!-- Checkout datepicker tweaks -->
<script type="text/javascript">
jQuery( document ).ready( function ( e ) {
if ( jQuery( '.checkout-date-picker' ).length ) {
var array = [
"01/01/2020","04/12/2020","05/10/2020","06/21/2020","07/04/2020","09/07/2020","11/26/2020","12/25/2020","12/26/2020",
"01/01/2021","04/04/2021","05/09/2021","06/20/2021","07/04/2021","09/06/2021","11/25/2021","12/25/2021","12/26/2021",
"01/01/2022","04/17/2022","05/08/2022","06/19/2022","07/04/2022","09/05/2022","11/24/2022","12/25/2022","12/26/2022",
"01/01/2023","04/09/2023","05/14/2023","06/18/2023","07/04/2023","09/04/2023","11/23/2023","12/25/2023","12/26/2023",
"01/01/2024","03/31/2024","05/12/2024","06/16/2024","07/04/2024","09/02/2024","11/28/2024","12/25/2024","12/26/2024",
]
jQuery.datepicker.setDefaults({
minDate: 0,
maxDate: "+12m",
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('mm/dd/yy', date);
return [ array.indexOf(string) == -1 ]
}
});
jQuery( '.checkout-date-picker' ).datepicker( 'option', 'dateFormat', 'mm/dd/yy' );
}
});
</script>
<?php
}
} // End fs_custom_adjust_datepicker_range()
add_action( 'wp_footer', 'fs_custom_adjust_datepicker_range', 50 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment