Home / Archive / MemberPress: Only Allow Coupon Once Per User
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Only Allow Coupon Once Per User

Restrict coupon codes so a user can only use the coupon code once.

Code Preview
php
<?php
function custom_validate_coupon( $errors ) {
	global $wpdb;
	//If user is not logged in, then they've never used a coupon before
	if( !MeprUtils::is_user_logged_in() ) { return $errors; }
	$coupon_code = ( isset( $_POST['mepr_coupon_code'] ) && !empty( $_POST['mepr_coupon_code'] ) )?stripslashes( $_POST['mepr_coupon_code'] ):'';
	if( ( $coupon = MeprCoupon::get_one_from_code( $coupon_code ) ) && isset( $coupon->ID ) && $coupon->ID ) {
		$user = MeprUtils::get_currentuserinfo();
		$query = "SELECT COUNT(*) FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id = {$coupon->ID} AND user_id = {$user->ID} AND status in ('complete','confirmed')";
		$count = $wpdb->get_var( $query );
		if( $count ) {
			$errors[] = 'This coupon code has already been used';
		}
	}
	return $errors;
}
add_filter('mepr-validate-signup', 'custom_validate_coupon');

Comments

Add a Comment