Celebrate myCred's Birthday with a BANG! - Use "Birthday30" for 1 year & "Birthday50" for 3 year to get up to 50% off on Membership Plans.

How to restrict users from submitting a Gravity form with Insufficient Balance?

front page Forums Community How to restrict users from submitting a Gravity form with Insufficient Balance?

This topic contains 29 replies, has 7 voices, and was last updated by Avatar Ogunsipe Oluwaseun Isaac 5 months, 1 week ago.

Viewing 15 posts - 1 through 15 (of 30 total)
  • Author
    Posts
  • #13896
    Avatar
    Saumya Banthia

    Hey there, I’m a Newbie here. Just started using this amazing plugin. I came across this mycred filter, which I added to the functions.php of mycred on my site. I used this filter below:

    add_filter( 'mycred_add', 'restrict_negative_balances', 1, 3 );
    function restrict_negative_balances( $reply, $request, $mycred ) {
    if ( $reply === false ) return $reply;
    
    // The amount the user will gain / loose
    $amount = $mycred->number( $request['amount'] );
    
    // Users current balance
    $current_balance = $mycred->get_users_cred( $request['user_id'] );
    
    // If balance is zero – decline
    if ( $amount < $mycred->zero() && $current_balance <= $mycred->zero() ) return false;
    
    // If we are deducting points, make sure the amount will not take us below zero
    if ( $amount < $mycred->zero() && $current_balance+$amount < $mycred->zero() ) return false;
    
    return $reply;
    }

    I thought it’d prevent the user from submitting a Gravity form which deducts 100 points from their account, if they had insufficient points, but that didn’t happen. I’m puzzled. Any help would be really appreciated. Thanks a lot in advance for anwering/attempting to answer the question.

    #13955
    Avatar
    WPExperts

    Hi.

    Could you show me where you found this code so I can update it as it has an error in it.

    The code should be:

    add_filter( 'mycred_add', 'mycred_pro_restrict_negative_balances', 1, 3 );
    function mycred_pro_restrict_negative_balances( $reply, $request, $mycred ) {
    
    	if ( $reply === false || $request['amount'] > 0 ) return $reply;
    
    	extract( $request );
    
    	// Users current balance
    	$current_balance = $mycred->get_users_balance( $user_id, $type );
    
    	// If balance is zero – decline
    	if ( $current_balance <= 0 ) return false;
    
    	// If we are deducting points, make sure the amount will not take us below zero
    	if ( ( $current_balance - abs( $amount ) ) < 0 ) return false;
    
    	return $reply;
    
    }
    
    #13963
    Avatar
    Saumya Banthia

    Thanks Gabriel, thanks for the quick reply. Is there anything else that to add to the script, any customization to be done, except copying it to functions.php . What I mean to say is I just have to add the code to function.php and it’ll restrict the users from purchasing anything with the points, if they don’t have sufficient points am I right?? And you asked about the thread where I found the snippet the link is, http://codex.mycred.me/filters/mycred_add-2/ , Example 3. And thanks again for helping.

    #13968
    Avatar
    Saumya Banthia

    UPDATE : BTW I just put that code you in functions.php , but when a deduction, which was triggered by Gravity forms, it didn;t deny the Gravity form from being submitted, even though, the balance wasn’t enough. Please help.

    #13975
    Avatar
    Saumya Banthia

    I’d like to clarify it more by telling the exact situation, so it follows

    I’m currently testing my point based reward system website, which completes surveys for you and gives you your file if you have sufficient points. The user gets 50 points for signup and 5 points for logging in each day. Theoretically, what the above code should do is to stop the user from submitting a gravity form who has a balance lower than 100. Just a bit of info, just in case it matters, all of the pages require the user to be logged in.

    #13976
    Avatar
    Saumya Banthia

    I’m real confused, as to where these things have to be added,

    /**
    * Global Redirect
    * @since 0.1
    * @version 1.0
    */
    add_action( ‘template_redirect’, ‘mycred_pro_force_buy_page_redirect’, 99 );
    function mycred_pro_force_buy_page_redirect()
    {
    // Make sure myCRED is enabled
    if ( ! function_exists( ‘mycred_get_users_cred’ ) ) return;

    // Ignore ajax calls and admin pages
    if ( ( defined( ‘DOING_AJAX’ ) && DOING_AJAX ) || is_admin() ) return;

    // Page ID to redirect users to
    $redirect_to_page = 10;

    // If user is logged in and we are not on the page we want to redirect to
    if ( is_user_logged_in() && ! is_page( $redirect_to_page ) ) {
    // Check for negative balances
    if ( mycred_get_users_cred( get_current_user_id() ) < = 0 ) {
    wp_redirect( get_permalink( $redirect_to_page ) );
    exit;
    }
    }
    }

    add_filter( ‘mycred_add’, ‘mycred_pro_restrict_negative_balances’, 1, 3 );
    function mycred_pro_restrict_negative_balances( $reply, $request, $mycred ) {

    if ( $reply === false || $request[‘amount’] > 0 ) return $reply;

    extract( $request );

    // Users current balance
    $current_balance = $mycred->get_users_balance( $user_id, $type );

    // If balance is zero – decline
    if ( $current_balance <= 0 ) return false;

    // If we are deducting points, make sure the amount will not take us below zero
    if ( ( $current_balance – abs( $amount ) ) < 0 ) return false;

    return $reply;

    }

    I’d also like to mention that when I edited these changes in the functions.php file, the functions.php became inactive. I’m really confused, sorry if I’m being terrible at it cause I really am a Newbie. I’d be really greatful, if you could please instruct me how could I add the two changes to functions.php without rendering it inactive. Sorry and thanks in advance again. Here’s a link to the screenshot: http://i.imgur.com/wU2Voj3.png?1

    #14060
    Avatar
    WPExperts

    Hi.

    So the above code example does not restrict the form from being submitted. I do not know how Gravity Forms would allow us to stop a form from being submitted. The above example that I gave goes into your theme’s function.php file and will will stop myCRED from taking points from a user if their balance is too low.

    You should contact Gravity Forms and see if there is a way for us to stop a form from being submitted based on custom requirements. Maybe there is a validation function we can utalize for this. Again I am no expert on GF.

    #14074
    Avatar
    Saumya Banthia

    The point deduction is triggered by the submission of the Gravity Form, so it should work with this code. And what about the functions.php becoming inactive as I mentioned above? Thanks

    #14077
    Avatar
    Saumya Banthia

    I’m sorry man, I am a real dumb-a**, I didn’t know I had to place it in the theme’s function.php I instead placed it in the plugin’s function.php It worked great.But the GF still gets submitted, although the balance doesn’t get deducted. This has half solved my problem. BTW

      I’ve thought a way to fully fix the problem. For that I’d like to know, if there is a way to allow access to a certain page only if the user has a specific balance, say 100 points???

    Thanks again and sorry for my misunderstanding. I’ll not promise but if my site works good with the point system and all in action, I’d like to donate to you. You guys are really doing an awesome work.

    #14078
    Avatar
    WPExperts

    Well it depends on how you want to restrict access.

    Are you looking to redirect users away from the page or would you be happy with the page loading but for example the content (where the form resides) would be hidden?

    #14081
    Avatar
    Saumya Banthia

    I wanna redirect the user to the Accounts page where he can choose whether to buy or earn points from just that specific page. The user should still be able to view other pages, such as the page where he buys stuff or completes jobs to earn points.

    #14083
    Avatar
    WPExperts

    Can you provide me with the page ID for:

    – The page that you want to enforce a minimum balance requirement for and
    – The page that you want them to be redirected to.

    #14089
    Avatar
    Saumya Banthia

    The page i want to enforce the min. Bal. Requirement is 140 and the my account page id is 22. Thanks for the quick reply, sorry I just fell asleep on my lappy.

    #14092
    Avatar
    Saumya Banthia

    I combined two of your codes to make up a code, but that didn’t work out. What I wanted to do with the below code was to redirect the user to page id = 22 in case he tried to access the page with id=40 given that the users balance was less than 100 points. :

    add_action( ‘template_redirect’, ‘mycred_pro_force_buy_page_redirect’, 99 );
    function mycred_pro_force_buy_page_redirect()
    {
    // Make sure myCRED is enabled
    if ( ! function_exists( ‘mycred_get_users_cred’ ) ) return;
    
    // Ignore ajax calls and admin pages
    if ( ( defined( ‘DOING_AJAX’ ) && DOING_AJAX ) || is_admin() ) return;
    
    // Users current balance
    $current_balance = $mycred->get_users_balance( $user_id, $type );
    
    // Page ID to redirect users to
    $redirect_to_page = 22;
    
    if ( $current_balance < 100 ){
    if (is_page( 140 )) {
    wp_redirect( get_permalink( $redirect_to_page ) );
    exit;
    }
    }
    }
    #14170
    Avatar
    WPExperts

    Sorry for the delay. Here is how I would do it: http://pastebin.com/tBq4VA9A

Viewing 15 posts - 1 through 15 (of 30 total)

You must be logged in to reply to this topic.

11