Php 根据电子商务中允许的时间范围禁用签出
我需要一个与时间相关的结账选项(因此,如果人们从过去的Php 根据电子商务中允许的时间范围禁用签出,php,wordpress,datetime,woocommerce,cart,Php,Wordpress,Datetime,Woocommerce,Cart,我需要一个与时间相关的结账选项(因此,如果人们从过去的23.59订购到06.00,结账将被取消,并重定向到其他页面)。我知道如何构建逻辑,但不知道时间关系的代码 这种与时间相关的结帐只需要在购物车中有特定类别时进行 这是我目前的代码: foreach( WC()->cart->get_cart() as $cart_item ){ if( is_checkout() && has_parent_term( $cart_item['product_id'])
23.59
订购到06.00
,结账将被取消,并重定向到其他页面)。我知道如何构建逻辑,但不知道时间关系的代码
这种与时间相关的结帐只需要在购物车中有特定类别时进行
这是我目前的代码:
foreach( WC()->cart->get_cart() as $cart_item ){
if( is_checkout() && has_parent_term( $cart_item['product_id']) && TIME_IS > (SATURDAY 23:59) && TIME_IS < (SUNDAY 06:00)) {
wp_redirect( get_permalink( get_option('woocommerce_cart_page_id') ) );
wc_add_notice( sprintf( 'unfortunatly you are not able to puchase these items at this time'), 'error');
exit;
}
}
foreach(WC()->cart->get\u cart()作为$cart\u项目){
如果(是checkout()&有父项($cart\u item['product\u id')和时间>星期六23:59)和时间<(星期日06:00)){
wp_重定向(get_permalink(get_选项('woocommerce_cart_page_id'));
wc_add_notice(sprintf(‘不幸的是,此时您无法将这些项目分段’),‘error’);
出口
}
}
条件函数
具有\u parent\u term()
在中定义。以下代码将检查购物车项目中定义的产品类别和时间范围。如果找到定义的产品类别,并且如果时间超出定义的时间范围(从06:00
到23:59
),将显示一个自定义通知,以避免结帐
使用woocommerce\u check\u cart\u项目时,不需要重定向到购物车
专用挂钩
对于时间范围条件函数:-格式化的开始和结束时间字符串需要类似于
hh:mm:ss
-您必须定义时区(请参见 2)仅针对周末天数(示例):
return($start>=$now&&end<$now&&in_数组(日期('w')、数组('6')、'0'))?真:假;
2)仅针对工作日(示例):
return($start>=$now&&end)<$now&&!在数组中(日期('w')、数组('6','0'))?真:假;
updated希望您理解:)@RMCS请考虑一下:仅用于测试,您将时区设置为自己的时区:…对于周五和周六,请使用array('5','6')
…最后一件事,请清除所有最后的注释。谢谢
// Custom conditional function that checks for parent product categories from a product category slug
function has_parent_term( $product_id, $category_slug ) {
// Convert category term slug to term id
$category_id = get_term_by('slug', $category_slug, 'product_cat')->term_id;
$parent_term_ids = array(); // Initializing
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id;
}
}
return in_array( $category_id, array_unique($parent_term_ids) );
}
// Custom conditional function that checks from a time range
function is_on_time( $start_time, $end_time, $time_zone = 'UTC' ) {
// Set the default time zone (http://php.net/manual/en/timezones.php)
date_default_timezone_set($time_zone);
$from = explode( ':', $start_time ); //
$from_h = isset($from[0]) ? $from[0] : 0; // hours
$from_m = isset($from[1]) ? $from[1] : 0; // minutes
$from_s = isset($from[2]) ? $from[2] : 0; // seconds
$start = mktime( $from_h, $from_m, $from_s, date("m"), date("d"), date("Y"));
$to = explode( ':', $end_time );
$to_h = isset($to[0]) ? $to[0] : 0; // hours
$to_m = isset($to[1]) ? $to[1] : 0; // minutes
$to_s = isset($to[2]) ? $to[2] : 0; // seconds
$end = mktime( $to_h, $to_m, $to_s, date("m"), date("d"), date("Y"));
$now = strtotime("now");
return ( $start >= $now && $end < $now ) ? true : false;
}
// Checking cart items and avoid checkout displaying an error notice
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
$found = false; // Initializing
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for product category term and parent term
if ( has_parent_term( $cart_item['product_id'], 't-shirts' ) )
$found = true; // category is found
}
// Checking product category and time
if ( $found && ! is_on_time( '6:00', '23:59', 'Europe/Paris' ) ){
// Avoiding checkout displaying a custom error notice
wc_add_notice( __("Sorry too late, time is now off. You are not allowed to checkout", "woocommerce" ), 'error' );
}
}
return ( $start >= $now && $end < $now && date('w') == '0' ) ? true : false;
return ( $start >= $now && $end < $now && in_array( date('w'), array('6','0') ) ) ? true : false;
return ( $start >= $now && $end < $now && ! in_array( date('w'), array('6','0') ) ) ? true : false;