Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save warnakey/db3b11510ae1c27e9f6c5d5b38284ca2 to your computer and use it in GitHub Desktop.
Save warnakey/db3b11510ae1c27e9f6c5d5b38284ca2 to your computer and use it in GitHub Desktop.
<!-- You could put this in your footer.php file near the bottom -->
<!--Function to find out what time it is, so we can disable some shipping options from Friday 2pm EST through Monday at midnight EST.-->
<?php if( $post->ID == 2305 || $post->ID == 2309) { // **REPLACE** these IDs with your CART and CHECKOUT pages! *important*
// Function to get the day of the week. Returns 0 through 6. Sunday = 0, Saturday = 6.
function getWeekday($date) {
return date('w', strtotime($date));
}
// You can find out what day of the week any date is like this
// echo "The day of the week is: ";
// echo getWeekday('2021-08-18') . '<br />';
// variable for grabbing current date and time
// $currentdatetime = date('Y-m-d H:i:s', current_time('timestamp', 1)); // Returns format of 2021-08-18 19:09:54
// variable for grabbing current date
$currentdate = date('Y-m-d', current_time('timestamp', 1)); // Returns format of 2021-08-18
// variable for grabbing current time in 24 hour format
// $currenttime = date('H:i:s', current_time('timestamp', 1)); // Returns format of 19:09:54
// echo "The current date and time is: ";
// echo $currentdatetime . '<br />'; // Returns format of 2021-08-18 19:09:54
// echo "The current date is: ";
// echo $currentdate . '<br />'; // Returns format of 2021-08-18
// echo "The current time is: ";
// echo $currenttime . '<br />'; // Returns format of 19:09:54
// What day of the week is it right now?
$currentdayoftheweek = getWeekday($currentdate);
// echo "The day of the week is: ";
// echo $currentdayoftheweek . '<br />'; // Returns a number like '3'
// Make a variable containing the day of the week and the time for the IF statements below
$currentdayofweekandtime = $currentdayoftheweek . ' ' . $currenttime;
// echo "The day of the week and time is: ";
// echo $currentdayofweekandtime . '<br />'; // Returns something like 3 19:09:54
// Spoof the day of week and time for testing purposes
// $currentdayofweekandtime = '0 19:00:01';
// If it is after SUNDAY at midnight (Greenwich mean time) but before Monday at midnight EST (4am Greenwich mean time)
// Or if it is after FRIDAY at 2pm EST (6pm Greenwich mean time)
if($currentdayofweekandtime <= '1 04:00:00' || $currentdayofweekandtime >= '5 18:00:00') { ?>
<script>
// **REPLACE** the IDs below with the IDs of the shipping options you want to disable
// Notice the 'parentElement' in each, that's because WooCommerce does something like this
// so you can't just target the <li> itself
// <ul>
// <li>
// <input type="radio" name="shipping_method[0]" id="shipping_method_0" value="method0">
// <label for="shipping_method_0">Free US Shipping (UPS)</label>
// </li>
// </ul>
// check that the shipping elements exist before hiding them to prevent errors
var shipping_method_0Exists = document.getElementById("shipping_method_0");
if(shipping_method_0Exists) {
document.getElementById("shipping_method_0").parentElement.style.display = "none";
}
var shipping_method_1Exists = document.getElementById("shipping_method_1");
if(shipping_method_1Exists) {
document.getElementById("shipping_method_1").parentElement.style.display = "none";
}
// This jQuery listens for when the checkout has been updated and hides the shipping options
// This is needed in addition to the lines above to work for the cart and checkout pages
jQuery(document.body).on('updated_checkout', function(){
// Don't forget to **REPLACE** the IDs below as well
// check that the shipping elements exist before hiding them to prevent errors
var shipping_method_0Exists = document.getElementById("shipping_method_0");
if(shipping_method_0Exists) {
document.getElementById("shipping_method_0").parentElement.style.display = "none";
}
var shipping_method_1Exists = document.getElementById("shipping_method_1");
if(shipping_method_1Exists) {
document.getElementById("shipping_method_1").parentElement.style.display = "none";
}
});
</script>
<?php }
} ?>
<!-- END OF Function to disable shipping options based on specific days and times -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment