WooCommerce 中的优惠券每日时间范围 [英] Coupon daily time range in WooCommerce

查看:35
本文介绍了WooCommerce 中的优惠券每日时间范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Woocommerce 中启用优惠券的使用时间范围,但没有成功.

I'm tying to enable the use of coupons to a range of hours in Woocommerce without success.

基于 根据 Woocommerce 中每日时间范围对特定产品的折扣,我的代码是:

// Utility function that gives the discount daily period
function get_discount_period_rate(){
    // Set the correct time zone  (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/Paris');


    // Set the start time and the end time
    $start_time = mktime( 08, 00, 00, date("m")  , date("d"), date("Y") );
    $end_time   = mktime( 09, 00, 00, date("m")  , date("d"), date("Y") );
    $time_now   = strtotime("now");
}

    // Set the coupon Ids that will be discounted
  $wc_coupon = new WC_Coupon('integralia10'); // get intance of wc_coupon which code is "integralia10"
    if (!$wc_coupon || !$wc_coupon->is_valid()) {
        return;
    }

    $coupon_code = $wc_coupon->get_code();
    if (!$coupon_code) {
        return;
    }

另外,我想使用 wc_print_notices 函数在有人尝试使用超出时间范围的优惠券代码时显示一条消息.

Also, I Would like to use the wc_print_notices function to show a message when somemeone try to use the Coupon code out of time range.

有什么建议吗?

推荐答案

使用以下代码,所有在特定时间范围内的优惠券都将有效,否则将显示错误消息.

With the following code, all coupons that fall within a certain time frame will be valid, if not, an error message will be displayed.

请设置:

  • 正确的时区
  • 开始和结束时间
function time_range() {
    // Set the correct time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set( 'Europe/Brussels' );

    // Set the start time and the end time to be valid
    $start_time = mktime( 11, 00, 00, date( 'm' ), date( 'd' ), date( 'y' ) );
    $end_time   = mktime( 13, 00, 00, date( 'm' ), date( 'd' ), date( 'y' ) );
    $time_now   = strtotime( 'now' );
    
    // Return true or false
    return $start_time <= $time_now && $end_time >= $time_now ? true : false;
}

// Is valid
function filter_woocommerce_coupon_is_valid( $is_valid, $coupon, $discount ) {      
    // Call function, return true or false
    return time_range();
}
add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );

// Error
function filter_woocommerce_coupon_error( $err, $err_code, $coupon ) {  
    // Validation
    if ( intval( $err_code ) === WC_COUPON::E_WC_COUPON_INVALID_FILTERED && time_range() == false ) {
        $err = __( 'My error', 'woocommerce' );
    }
    
    return $err;
}
add_filter( 'woocommerce_coupon_error', 'filter_woocommerce_coupon_error', 10, 3 );



更新:要应用相同但仅适用于某些优惠券 ID,改用这个.



UPDATE: To apply the same, but only to certain coupons ID's, use this instead.

function time_range_coupon_id( $coupon_id ) {
    // For specific coupon ID's only, several could be added, separated by a comma
    $specific_coupons_ids = array( 107, 108 );
    
    // Coupon ID in array, so check
    if ( in_array( $coupon_id, $specific_coupons_ids ) ) {
        // Set the correct time zone (http://php.net/manual/en/timezones.php)
        date_default_timezone_set( 'Europe/Brussels' );

        // Set the start time and the end time to be valid
        $start_time = mktime( 12, 00, 00, date( 'm' ), date( 'd' ), date( 'y' ) );
        $end_time   = mktime( 15, 00, 00, date( 'm' ), date( 'd' ), date( 'y' ) );
        $time_now   = strtotime( 'now' );
        
        // Return true or false
        return $start_time <= $time_now && $end_time >= $time_now ? true : false;
    }
    
    // Default
    return true;
}

// Is valid
function filter_woocommerce_coupon_is_valid( $is_valid, $coupon, $discount ) {
    // Get coupon ID
    $coupon_id = $coupon->get_id();
    
    // Call function, return true or false
    return time_range_coupon_id( $coupon_id );
}
add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );

// Error
function filter_woocommerce_coupon_error( $err, $err_code, $coupon ) {
    // Get coupon ID
    $coupon_id = $coupon->get_id();
    
    // Validation
    if ( intval( $err_code ) === WC_COUPON::E_WC_COUPON_INVALID_FILTERED && time_range_coupon_id( $coupon_id ) == false ) {
        $err = __( 'My error', 'woocommerce' );
    }
    
    return $err;
}
add_filter( 'woocommerce_coupon_error', 'filter_woocommerce_coupon_error', 10, 3 );

这篇关于WooCommerce 中的优惠券每日时间范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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