Created
July 24, 2020 07:10
-
-
Save webdados/907cde21ce1119e5f64eace2c1c8da5b to your computer and use it in GitHub Desktop.
Close the WooCommerce shop on certain days of the week at a certain time in the day
This file contains hidden or 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 | |
/* Hire me at webdados.pt for more WooCommerce magic */ | |
//The function that determines if the shop is closed or not | |
function custom_shop_closed() { | |
//Week days closed - ISO-8601 numeric representation of the day of the week - 1 (for Monday) through 7 (for Sunday) | |
$week_days_closed = array( 5, 6 ); //Fridays and Saturdays | |
//Hours closed | |
$min_hour_close = '07:00'; | |
$max_hour_close = '09:00'; | |
//Test it | |
if ( | |
in_array( date_i18n( 'N' ), $week_days_closed ) | |
&& | |
date_i18n( 'H:i' ) >= $min_hour_close | |
&& | |
date_i18n( 'H:i' ) <= $max_hour_close | |
) { | |
return true; | |
} | |
return false; | |
} | |
//Make products not purchasable | |
add_filter( 'woocommerce_is_purchasable', function() { | |
return !custom_shop_closed(); | |
} ); | |
//Add a store notice | |
add_action( 'woocommerce_init', function() { | |
if ( custom_shop_closed() ) | |
wc_add_notice( 'Yippee ki-yay - The shop is closed!', 'notice' ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment