【问题标题】:Coupon daily time range in WooCommerceWooCommerce 中的优惠券每日时间范围
【发布时间】:2021-12-27 14:14:18
【问题描述】:

我正在尝试在 Woocommerce 中启用优惠券的使用时间,但没有成功。

基于Discount on specific products based on a daily time range in 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 函数显示一条消息。

有什么建议吗?

【问题讨论】:

    标签: php wordpress woocommerce date-range coupon


    【解决方案1】:

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

    请设置:

    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, 改用这个。

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

    【讨论】: