在特定日期和时间范围内销售特定的 Woocommerce 商品 [英] Sell specific Woocommerce items on a specific day and others on a time range

查看:36
本文介绍了在特定日期和时间范围内销售特定的 Woocommerce 商品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前问过是否可以限制特定物品只能在周日购买,"仅在 WooCommerce 中的特定日期销售某些产品" 如果答案代码效果很好......

I previously asked if specific items could be restricted to be purchasable on sundays only and the "Sell some products only on a specific day in WooCommerce" provided answer code worked great…

我现在有些商品可以在任何一天购买,但只能在伦敦时间下午 12:00-2.30 之间购买.

I now have certain items that can be purchased any day but should only be allowed to be purchased between 12:00-2.30 PM London Time.

你能帮忙修改之前的代码以允许这样做吗?

Can you help modify the previous code to allow for this?

注意:我仍然想运行 LoicTheAztec 之前的代码,因为有些项目仍然限制在星期日.

NOTE: I still want to run the previous code by LoicTheAztec as some items are still restricted to Sunday.

推荐答案

以下代码将允许在星期日购买特定物品以及在某个时间范围(12 小时到 14 小时 30 分)购买特定物品.

The following code will allow to have specific items purchasable on sundays and specific items purchasable on a time range (12h to 14h30).

这是这两个答案代码的组合:

It is a combination of this 2 answers code:

代码如下:

// The "sunday" products (setting your product IDS in the array)
function sunday_products() {
    // HERE your product IDs in the array (need to be coma separated)
    return array( 37 );
}

// The 12h to 14h30 products (setting your product IDS in the array)
function time_range_products() {
    // HERE your product IDs in the array (need to be coma separated)
    return array( 53, 57 );
}

// Utility conditional function that check if day is sunday (returns boolean)
function is_sunday() {
    // Set Your shop time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/London');

    // If the current day is "sunday" return true (else retun false)
    return ( date('w') == 0 ) ? true : false;
}

// Utility conditional funtion for open hours (returns boolean true when store is open)
function in_time_range() {
    // Set Your shop time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/London');

    // Below your shop time and dates settings
    $open_time = mktime('12', '00', '00', date('m'), date('d'), date('Y')); // 12:00:00
    $end_time  = mktime('14', '30', '00', date('m'), date('d'), date('Y')); // 14:30:00
    $now       = time(); // Current timestamp in seconds

    return ( $now >= $start_time && $now <= $end_time ) ? true : false;
}


// Enable purchases for specific items on sundays only
add_filter( 'woocommerce_variation_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 );
function enable_specific_products_on_sundays( $purchasable, $product ) {
    // Enable purchases for specific defined item only on sunday
    if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) )
        $purchasable = false;

    // Enable purchases for specific defined item only from 12h to 14h30
    if( ! in_time_range() && in_array( $product->get_id(), time_range_products() ) )
        $purchasable = false;

    return $purchasable;
}

// Add a notice in specific products outside sundays
add_action( 'woocommerce_before_single_product', 'filter_before_single_product' );
function filter_before_single_product() {
    global $product;

    // For sundays product
    if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) ) {
        wc_print_notice( __( 'This product is only purchasable on sundays', 'woocommerce' ), 'error' );
    } 
    // For hour range product
    elseif( ! in_time_range() && in_array( $product->get_id(), time_range_products() ) ) {
        wc_print_notice( __( 'This product is only purchasable between 12 AM and 2h30 PM', 'woocommerce' ), 'error' );
    }
}

// IN CASE OF (but not needed): Cart and checkout validation + error message
add_action( 'woocommerce_check_cart_items', 'conditionally_allowing_checkout' );
add_action( 'woocommerce_checkout_process', 'conditionally_allowing_checkout' );
function conditionally_allowing_checkout() {
    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ){
        // Check cart items
        if( ! is_sunday() && in_array( $cart_item['data']->get_id(), sunday_products() ) ){
            wc_add_notice( sprintf(__("%s can be only purchase on sundays.", "woocommerce"), $cart_item['data']->get_name() ), 'error' );
            break;
        }
        else if( ! in_time_range() && in_array( $cart_item['data']->get_id(), time_range_products() ) ){
            wc_add_notice( sprintf(__("%s can be only purchase between 12 AM and 2h30 PM.", "woocommerce"), $cart_item['data']->get_name() ), 'error' );
            break;
        }
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中.它应该可以工作.

Code goes in function.php file of your active child theme (or active theme). It should work.

这篇关于在特定日期和时间范围内销售特定的 Woocommerce 商品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆