Home / Archive / MemberPress: Allow Coupon for Active Members Only
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Allow Coupon for Active Members Only

The code snippet will display the error if an inactive member (user/guest without any active subscriptions) tries to register using a specified coupon. 

Though users can apply the coupon (any discounts it provides will also be calculated), they won't be able to complete the checkout process. Instead, an error message will be displayed.
The example code will display the "This coupon code is only valid for active members" error message if the THECOUPONCODE coupon code is used.

The code snippet should be updated by replacing the THECOUPONCODE coupon code with the actual coupon, on this line:

define( 'ACTIVE_MEMBER_COUPON', 'THECOUPONCODE' );

To modify the error message displayed, adjust the This coupon code is only valid for active members text on this line:

$errors[] = "This coupon code is only valid for active members";

Code Preview
php
<?php
function mepr_validate_coupon_for_active_members($errors) {
    $user = new MeprUser(get_current_user_id());
    $coupon_code = (isset($_POST['mepr_coupon_code']) && !empty( $_POST['mepr_coupon_code'])) ? stripslashes( $_POST['mepr_coupon_code'] ) : '';
    // Define the coupon code as a constant
    define( 'ACTIVE_MEMBER_COUPON', 'THECOUPONCODE' );
    // Check if it's the specific coupon and the user has an active subscription
    if ($coupon_code == ACTIVE_MEMBER_COUPON && !$user->active_product_subscriptions()) { 
        $errors[] = "This coupon code is only valid for active members"; 
    }
    return $errors;
}
add_filter( 'mepr-validate-signup', 'mepr_validate_coupon_for_active_members' );

Comments

Add a Comment