【问题标题】:Restrict use of coupon if customer have used related coupons in previous orders in WooCommerce如果客户在之前的 WooCommerce 订单中使用过相关优惠券,则限制使用优惠券
【发布时间】:2021-06-07 08:38:50
【问题描述】:

我正在寻找一种方法来限制优惠券的使用,并在客户之前在 WooCommerce 中之前的订单中使用过相关优惠券时显示错误消息。

我所说的相关优惠券是指:优惠券代码与购物车/结帐页面上当前插入的优惠券一起出现在预定义数组中。在此基础上进行比较。

我的代码尝试:

add_filter( 'woocommerce_coupon_is_valid', 'specific_coupons_valid', 10,
3 );
function specific_coupons_valid( $is_valid, $coupon, $discount ){
     $coupon_codes = array( 'free10', 'free20', 'free30' );

     $args = array(
         'posts_per_page'   => -1,
         'orderby'          => 'title',
         'order'            => 'asc',
         'post_type'        => 'shop_coupon',
         'post_status'      => 'publish',
     );

     $coupons = get_posts( $args );

     if( in_array( $coupons, $coupon_codes ) ) {
         $is_valid = false;
     }

     return $is_valid;
}

感谢任何帮助

【问题讨论】:

    标签: php wordpress woocommerce orders coupon


    【解决方案1】:

    如果客户之前在 WooCommerce 中的先前订单中使用过相关优惠券,则要限制优惠券的使用,您可以使用:

    // Compare & return true OR false
    function compare_related_coupon_codes( $current_coupon_code ) {
        // Add multiple coupon codes to compare, seperated by a comma
        $compare_coupon_codes = array( 'coupon1', 'coupon2', 'coupon3' );
        
        // Default
        $valid = true;
        
        // When the current coupon code has to be compared with other coupon codes
        if ( in_array( $current_coupon_code, $compare_coupon_codes ) ) {        
            // Get user ID
            $user_id = get_current_user_id();
            
            // Get the WC_Customer instance Object
            $customer = New WC_Customer( $user_id );
            
            // Billing email
            $email = $customer->get_billing_email();
    
            // Loop through         
            foreach ( $compare_coupon_codes as $coupon_code ) {
                // Get the WC_Coupon instance Object
                $coupon_obj = New WC_Coupon( $coupon_code );
    
                // If one of the coupons has already been used by the customer
                if ( array_intersect( array( $user_id, $email ), $coupon_obj->get_used_by() ) ) {
                    $valid = false;
                    break;
                }   
            }
        }
        
        // Return
        return $valid;
    }
    
    // Valid
    function filter_woocommerce_coupon_is_valid( $is_valid, $coupon, $discount ) {
        if ( ! is_user_logged_in() ) return $is_valid;
        
        // Get current applied coupon code
        $current_coupon_code = strtolower( $coupon->get_code() );
    
        // Call function, true OR false
        return compare_related_coupon_codes( $current_coupon_code );
    }
    add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );
    
    // Error
    function filter_woocommerce_coupon_error( $err, $err_code, $coupon ) {
        if ( ! is_user_logged_in() ) return $err;
        
        // Get current applied coupon code
        $current_coupon_code = strtolower( $coupon->get_code() );
        
        // Validation, call function, true OR false
        if ( intval( $err_code ) === WC_COUPON::E_WC_COUPON_INVALID_FILTERED && compare_related_coupon_codes( $current_coupon_code ) == false ) {
            $err = __( 'My coupon error', 'woocommerce' );
        }
        
        return $err;
    }
    add_filter( 'woocommerce_coupon_error', 'filter_woocommerce_coupon_error', 10, 3 );
    

    通过我的答案中添加的评论标签进行解释

    相关:Coupon daily time range in WooCommerce

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      相关资源
      最近更新 更多