根据 Woocommerce 中允许的时间范围禁用结帐
Disable checkout based on an allowed time range in Woocommerce
我需要一个与时间相关的结账选项(所以如果人们从过去的 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;
}
}
条件函数 has_parent_term()
在 中定义。
以下代码将检查购物车项目中定义的产品类别和时间范围。如果找到定义的产品类别并且如果时间超出定义的时间范围 (从06:00
到23:59
), 自定义通知将被删除避免结帐。
There is no need of redirection to cart when using woocommerce_check_cart_items
dedicated hook.
对于时间范围条件函数:
- 格式化的开始和结束时间字符串需要像 hh:mm:ss
- 您必须定义时区(请参阅 List of Supported Timezones
// 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' );
}
}
代码进入您的活动子主题(活动主题)的 function.php 文件。已测试并有效。
当时间结束时,如果有任何购物车项目剩余到特定产品类别:
1) 在购物车页面:
2) 在结帐页面:
添加 - 在 is_on_time()
条件函数中定位星期几。
如果需要,请将 return ( $start >= $now && $end < $now ) ? true : false;
替换为以下内容之一:
1) 仅定位 'Sundays'(示例):
return ( $start >= $now && $end < $now && date('w') == '0' ) ? true : false;
2) 仅定位周末(示例):
return ( $start >= $now && $end < $now && in_array( date('w'), array('6','0') ) ) ? true : false;
2) 仅针对工作日(示例):
return ( $start >= $now && $end < $now && ! in_array( date('w'), array('6','0') ) ) ? true : false;
我需要一个与时间相关的结账选项(所以如果人们从过去的 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;
}
}
条件函数 has_parent_term()
在
以下代码将检查购物车项目中定义的产品类别和时间范围。如果找到定义的产品类别并且如果时间超出定义的时间范围 (从06:00
到23:59
), 自定义通知将被删除避免结帐。
There is no need of redirection to cart when using
woocommerce_check_cart_items
dedicated hook.
对于时间范围条件函数:
- 格式化的开始和结束时间字符串需要像 hh:mm:ss
- 您必须定义时区(请参阅 List of Supported Timezones
// 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' );
}
}
代码进入您的活动子主题(活动主题)的 function.php 文件。已测试并有效。
当时间结束时,如果有任何购物车项目剩余到特定产品类别:
1) 在购物车页面:
2) 在结帐页面:
添加 - 在 is_on_time()
条件函数中定位星期几。
如果需要,请将 return ( $start >= $now && $end < $now ) ? true : false;
替换为以下内容之一:
1) 仅定位 'Sundays'(示例):
return ( $start >= $now && $end < $now && date('w') == '0' ) ? true : false;
2) 仅定位周末(示例):
return ( $start >= $now && $end < $now && in_array( date('w'), array('6','0') ) ) ? true : false;
2) 仅针对工作日(示例):
return ( $start >= $now && $end < $now && ! in_array( date('w'), array('6','0') ) ) ? true : false;