对于一个非营利组织,我需要创建一个函数,首先检查时间,如果时间在范围内,检查是哪一天。他们只允许在给定时间内在Tuesdays
,Thursdays
和Saturdays
上购买。
我已经尝试了很多东西,* 基于时间的启用/禁用添加到购物车在Woocommerce* 答案启发了我的代码如下:
function opening_hours() {
date_default_timezone_set('Europe/Paris');
$opening_time = mktime('10', '00', '00', date('m'), date('d'), date('Y'));
$closing_time = mktime('17', '00', '00', date('m'), date('d'), date('Y'));
$now = time();
return ($now >= $opening_time && $now <= $closing_time) ? true : false;
}
add_filter( 'woocommerce_variation_is_purchasable', 'disable_purchases_on_closed_days', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'disable_purchases_on_closed_days', 10, 2 );
function disable_purchases_on_closed_days( $purchasable, $product ) {
// check if the time is correct for opening hours
if ( opening_hours() ) {
// if the time is right, make sure it is one of the correct days (tuesday, thursday or saturday)
if (date('N') != 2 || date('N') != 4 || date('N') != 6){
// if not the correct day, disable purchase
$purchasable = false;
return $purchasable;
}
}
}
add_action( 'woocommerce_check_cart_items', 'cart_and_checkout_opening_hours' );
add_action( 'woocommerce_checkout_process', 'cart_and_checkout_opening_hours' );
function cart_and_checkout_opening_hours() {
if ( ! opening_hours() ) {
wc_add_notice( __("The online store is currently closed. You can view products, but purchases are not allowed."), 'error' );
}
}
add_action( 'template_redirect', 'shop_is_closed_notice' );
function shop_is_closed_notice(){
if ( ! ( is_cart() || is_checkout() ) && !opening_hours() ) {
$message = esc_html__('The online store is currently closed. You can view products, but purchases are allowed between x-x.', 'woocommerce' );
wc_add_notice( '<span class="shop-closed">' . $message . '</span>', 'notice' );
}
}
但由于某种原因,我不能让它工作。我没有错误,也没有通知,无论我做什么,所有的产品都是不可购买的。
也许有人可以审查代码并给予一些见解?
1条答案
按热度按时间0dxa2lsx1#
代码中有一些错误和复杂性。尝试以下重新访问的代码:
代码进入函数。php文件您的活动子主题(或活动主题)。测试和作品。