php - 仅允许在 WooCommerce 中选定工作日的时间范围内进行购买

标签 php wordpress datetime woocommerce product

为了非营利目的,我需要创建一个函数,首先检查时间,如果时间在范围内,则检查今天是哪一天。他们只允许在指定时间内的星期二星期四星期六进行购买。

我尝试了很多事情, Time based Enable/Disable Add to Cart in 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' );
    }
}

但由于某种原因,我无法让它工作。我没有收到任何错误,也没有任何通知,无论我做什么,所有产品都是不可购买的。

也许有人可以查看代码并提供一些见解?

最佳答案

您的代码中存在一些错误和复杂性。尝试以下重新访问的代码:

// Custom conditional function
function is_shop_open() {
    date_default_timezone_set('Europe/Paris');

    $start_time   = mktime('10', '00', '00', date('m'), date('d'), date('Y')); // 10h
    $end_time     = mktime('17', '00', '00', date('m'), date('d'), date('Y')); // 17h
    $now_time     = time();
    $allowed_days = in_array( date('N'), array(2, 4, 6) ); // tuesdays, thursdays and saturdays

    return $allowed_days && $now_time >= $start_time && $now_time <= $end_time ? true : false;
}

add_filter( 'woocommerce_variation_is_purchasable', 'shop_closed_disable_purchases' );
add_filter( 'woocommerce_is_purchasable', 'shop_closed_disable_purchases' );
function shop_closed_disable_purchases( $purchasable ) {
    return is_shop_open() ? $purchasable : false;
}

add_action( 'woocommerce_check_cart_items', 'shop_open_allow_checkout' );
add_action( 'woocommerce_checkout_process', 'shop_open_allow_checkout' );
function shop_open_allow_checkout() {
    if ( ! is_shop_open() ) {
        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() ) && ! is_shop_open() ) {
        wc_add_notice( sprintf( '<span class="shop-closed">%s</span>',
            esc_html__('The online store is currently closed. You can view products. But purchases are allowed between x-x.', 'woocommerce' )
        ), 'notice' );
    }
}

代码位于事件子主题(或事件主题)的 function.php 文件中。经过测试并有效。

关于php - 仅允许在 WooCommerce 中选定工作日的时间范围内进行购买,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63071349/

相关文章:

WordPress 3.9 自定义帖子类型永久链接

javascript - jQuery ajax 完成在 WordPress 上总是失败

python - Pandas 每月营业日股价数据

php - PDO 从 Postgres 获取小数秒

php - 如何设置类别列表限制?

java - 没有时区的 Jaxb DateTime

r - 聚合行及时关闭

php - MySQL 错误号 :150 的更多详细信息

PHP:如何启动一个分离进程?

error-reporting - php ini_set 是如何工作的?