以下代码将限制您的商店营业时间为上午 12 点至下午 22 点,并在关闭时间禁用添加到购物车和结帐流程。
您必须在第一个功能设置中设置您的时区:
// Utility conditional funtion for store open hours (returns boolean true when store is open)
function is_store_open() {
// Set Your shop time zone (http://php.net/manual/en/timezones.php)
date_default_timezone_set('Europe/Paris');
// Below your shop time and dates settings
$start_time = mktime('12', '00', '00', date('m'), date('d'), date('Y')); // 12:00:00
$end_time = mktime('22', '00', '00', date('m'), date('d'), date('Y')); // 22:00:00
$now = time(); // Current timestamp in seconds
return ( $now >= $start_time && $now <= $end_time ) ? true : false;
}
// Disable purchases on closing shop time
add_filter( 'woocommerce_variation_is_purchasable', 'disable_purchases_on_shop', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'disable_purchases_on_shop', 10, 2 );
function disable_purchases_on_shop( $purchasable, $product ) {
// Disable purchases on closing shop time
if( ! is_store_open() )
$purchasable = false;
return $purchasable;
}
// Cart and checkout validation
add_action( 'woocommerce_check_cart_items', 'conditionally_allowing_checkout' );
add_action( 'woocommerce_checkout_process', 'conditionally_allowing_checkout' );
function conditionally_allowing_checkout() {
if ( ! is_store_open() ) {
// Store closed
wc_add_notice( __("The Store is Closed… Purchases are allowed from 12:00 AM to 22:00 PM"), 'error' );
}
}
add_action( 'template_redirect', 'closing_shop_notice' );
function closing_shop_notice(){
if ( ! ( is_cart() || is_checkout() ) && ! is_store_open() ) {
// Store closed notice
wc_add_notice( __("The Store is Closed… Purchases are allowed from 12:00 AM to 22:00 PM"), 'notice' );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。