Last active
November 21, 2016 17:16
-
-
Save Dilden/e1fa597ee0b7ecf716a85956ab4a5e93 to your computer and use it in GitHub Desktop.
Restrict dates on jQuery UI Datepicker within WordPress/WooCommerce checkout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
// AJAX request to disable days on calendar in checkout | |
add_action( 'wp_ajax_company_holidays', 'company_holidays_callback' ); | |
add_action( 'wp_ajax_nopriv_company_holidays', 'company_holidays_callback' ); | |
function company_holidays_callback() { | |
$holidays = []; | |
$currentYear = $limitYear = (int) date('Y'); | |
do { | |
// New Years | |
$x = date('N', strtotime("first day of january $currentYear")); | |
$day = date('d', strtotime("first day of january $currentYear")); | |
array_push($holidays, [1, (int) ($day + fri_or_mon($x)), $currentYear]); | |
// President's Day | |
$day = date('d', strtotime("third monday of february $currentYear")); | |
array_push($holidays, [2, (int) $day, $currentYear]); | |
// Good Friday | |
$month = date('m', strtotime("-2 day", easter_date($currentYear))); | |
$day = date('d', strtotime("-2 day", easter_date($currentYear))); | |
array_push($holidays, [(int) $month, (int) $day, $currentYear]); | |
// Memorial Day | |
$day = date('d', strtotime("last Monday of May $currentYear")); | |
array_push($holidays, [5, (int) $day, $currentYear]); | |
// July 4th | |
$x = (int) date('N', strtotime("3 day", strtotime("July $currentYear"))); | |
$day = (int) date('d', strtotime("3 day", strtotime("July $currentYear"))); | |
array_push($holidays, [7, $day, $currentYear]); | |
// Labor Day | |
$day = date('d', strtotime("first Monday of September $currentYear")); | |
array_push($holidays, [9, (int) $day, $currentYear]); | |
// Veteran's Day | |
$x = date('N', strtotime("10 day", strtotime("November $currentYear"))); | |
$day = date('d', strtotime("10 day", strtotime("November $currentYear"))); | |
array_push($holidays, [11, (int) ($day + fri_or_mon($x)), $currentYear]); | |
// Thanksgiving + Black Friday | |
$day = date('d', strtotime("fourth Thursday of November $currentYear")); | |
array_push($holidays, [11, (int) $day, $currentYear]); | |
array_push($holidays, [11, (int) $day + 1, $currentYear]); | |
// Xmas eve | |
$day = date('d', strtotime("last day of December $currentYear")); | |
$day = ((int) $day) - 7; | |
array_push($holidays, [12, $day, $currentYear]); | |
// Xmas | |
$twenty_fifth = strtotime("-6 day", strtotime("last day of December $currentYear")); | |
$x = date('N', $twenty_fifth); | |
$day = date('d', $twenty_fifth); | |
array_push($holidays, [12, $day + fri_or_mon($x), $currentYear]); | |
$currentYear++; | |
} while ($currentYear < $limitYear + 2); | |
wp_send_json($holidays); | |
} | |
// Accepts a numerical value for the day of the week. Returns # of days to be added | |
function fri_or_mon($dayofweek) { | |
if($dayofweek == 6) { | |
// we get the previous Friday off | |
return -1; | |
} | |
elseif($dayofweek == 7) { | |
// we get the following Monday off | |
return 1; | |
} | |
else { | |
return 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready( function() { | |
function nationalDays(date) { | |
for (i = 0; i < natDays.length; i++) { | |
if (date.getMonth() === natDays[i][0] - 1 | |
&& date.getDate() === natDays[i][1] && date.getFullYear() === natDays[i][2]) { | |
return [false]; | |
} | |
} | |
return [true]; | |
} | |
function noWeekendsOrHolidays(date) { | |
var noWeekend = jQuery.datepicker.noWeekends(date); | |
if (noWeekend[0]) { | |
return nationalDays(date); | |
} else { | |
return noWeekend; | |
} | |
} | |
if(jQuery("#order_fulfillment_date").length) { | |
var natDays = []; | |
jQuery.post(ajax_object.ajax_url, {action: 'company_holidays'}, function(response) { | |
natDays = response; | |
}) | |
.fail(function(response) { | |
natDays = []; | |
console.log("error with dates in calendar"); | |
}); | |
} | |
// Datepicker on checkout | |
jQuery("#order_fulfillment_date").datepicker({ | |
minDate: 1, | |
maxDate: 365, | |
beforeShowDay: noWeekendsOrHolidays | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment