'Restrict use of coupon if customer have used related coupons in previous orders in WooCommerce

I am looking for a way, to limit coupon usage and to display an error message if a customer have used related coupons in previous orders before in WooCommerce.

By related coupons, I mean: couponcodes that appear in a predefined array along with the currently inserted coupon on the cart/checkout page. On that basis, compare.

My code attempt:

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

Any help is appreciated



Solution 1:[1]

To limit coupon usage if a customer have used related coupons in previous orders in WooCommerce before, you can use:

// 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( $valid, $coupon, $discount ) {
    if ( ! is_user_logged_in() ) return $valid;
    
    // Get current applied coupon code
    $current_coupon_code = strtolower( $coupon->get_code() );

    // Call function, true OR false
    $valid = compare_related_coupon_codes( $current_coupon_code ); 

    // NOT valid
    if ( ! $valid ) {
        throw new Exception( __( 'My error', 'woocommerce' ), 109 );
    }

    return $valid;
}
add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );

Explanation via comment tags added in my answer

Related: Coupon daily time range in WooCommerce

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1