允许在 WooCommerce 的时间范围内购买特定产品

时间:2021-04-26 05:20:15

标签: php wordpress datetime woocommerce product

根据 Only allow purchases on a time range for selected week days in WooCommerce 的回答,我正在尝试修改代码以仅将其应用于两个产品 ID,而其他所有产品 ID 都应该可以随时购买:

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

    $start_time = mktime('10', '00', '00', date('m'), date('d'), date('Y'));

    $end_time = mktime('14', '30', '00', date('m'), date('d'), date('Y'));

    $now_time = time();

    $allowed_days = in_array( date('N'), array(1, 2, 3, 4, 5) );

    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 ) {

    // I am trying to apply the open and closed to two products
    if (is_product('1') || is_product('2') )
    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( __("Notice text here"), 'notice' );
    }
}

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__('Notice text here', 'woocommerce' )
    ), 'notice' );

    }
}

但是我不能让它工作。关于我哪里出错的任何想法?

1 个答案:

答案 0 :(得分:2)

要仅允许在特定时间范围内购买特定产品,请改用以下内容:

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

    $start_time   = mktime('10', '00', '00', date('m'), date('d'), date('Y'));
    $end_time     = mktime('14', '30', '00', date('m'), date('d'), date('Y'));
    $now_time     = time();
    $allowed_days = in_array( date('N'), array(1, 2, 3, 4, 5) );

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

add_filter('woocommerce_variation_is_purchasable', 'shop_closed_disable_purchases', 10, 2 );
add_filter('woocommerce_is_purchasable', 'shop_closed_disable_purchases', 10, 2 );
function shop_closed_disable_purchases( $purchasable, $product ) {
    $targeted_ids = array(37, 53); // Here set the targeted products
    
    if ( in_array($product->get_id(), $targeted_ids) ) {
        return is_shop_open() ? $purchasable : false;
    }
    return $purchasable;
}

// Optional
add_action( 'woocommerce_check_cart_items', 'shop_open_allow_checkout' );
add_action( 'woocommerce_checkout_process', 'shop_open_allow_checkout' );
function shop_open_allow_checkout(){
    $targeted_ids = array(37, 53); // Here set the targeted products
    
    foreach( WC()->cart->get_cart() as $item ) {
        if ( array_intersect( array($item['product_id'], $item['variation_id']), $targeted_ids ) && ! is_shop_open() ) {
            wc_add_notice( __("Notice text here"), 'error' );
        }
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。