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 - 16 through 30 (of 30 total)
  • Author
    Posts
  • #14178
    Avatar
    Saumya Banthia

    Will try the code and update you ASAP, thanks for the reply though. Thanks for taking out time.

    #14183
    Avatar
    Saumya Banthia

    Gabriel, thank you so much, it worked like a charm!!

    #14184
    Avatar
    Saumya Banthia

    You’re an awesome person, if I earn good from this site I’d like to push in a donation, so just mail me your paypal [email removed] or write it here itself, whenever you feel free. I’ll be pushing the donation anytime. But just as I said no promises. But I really do appreciate the time you took out for resolving my issue personally.

    #14189
    Avatar
    WPExperts

    Excellent news. Glad to have helped. You can always made donations here.

    #14622
    Avatar
    plaxant

    Gabriel, how would you modify that to target multiple pages?

    #14624
    Avatar
    WPExperts

    @plaxant – You can use an array of page / post IDs to check for instead of just one.

    This is done here:

    if ( ! is_page( 40 ) ) return;
    

    This will check is this is page 40. WordPress documentation for is_page tells us that we can either pass a numeric ID, a page slug or an array of variables.

    Example:

    if ( ! is_page( array( 10, 20, 30, 40 ) ) return;
    

    You can also use other WordPress Conditional Tags here like for example forcing this on an entire category:

    if ( is_category( 'premium' ) ) return;
    
    #15028
    Avatar
    Erik

    Here is a snippet that may benefit anyone reading this thread. In this example, Field 1 should contain the # of credits that will be consumed when the form is submitted. If the current user’s account doesn’t have enough credits to cover this number of credits requested in this field’s value, the user will not be allowed to proceed.

    // Validate quantity of credits used against current account balance
    add_filter( 'gform_validation', 'custom_validation' );
    function custom_validation( $validation_result ) {
        $form = $validation_result['form'];
    
            // Grab user ID
    	$user_id = get_current_user_id();
            // Grab current credit balance
    	$balance = mycred_get_users_cred( $user_id );
    
        // If Field 1 is greater than the available balance, fail validation and return an error.
        if ( rgpost( 'input_1' ) > $balance ) {
    
            // set the form validation to false
            $validation_result['is_valid'] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach( $form['fields'] as &$field ) {
    
                //NOTE: replace 1 with the field you would like to validate
                if ( $field->id == '1' ) {
                    $field->failed_validation = true;
                    $field->validation_message = 'Not enough credits.';
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result['form'] = $form;
        return $validation_result;
    
    }
    
    
    #16381
    Avatar
    black9design

    I’m wondering if you can also provide code to allow user onto the page but to show an error on the form saying the account balance is not enough.

    Not like the one above…I don’t want them to fill out the form first. But right away to check and throw an error if not enough points.

    #16382
    Avatar
    black9design

    Can you also provide a snippet that will output the current user points and allow me to add that value to a field?

    such as

    <div class=”one-half first”>
    Points required = xx
    </div>

    <div class=”one-half”>
    Your Points = xx
    </div>

    #17423
    Avatar
    Suman Dahal

    Thank You Gabriel, It Was Awesome Code. It Did Worked Perfectly. But Can U Write The Code Which Does the Following :-

    1) After Validation, The Validated Amount Would Be Deducted From User Balance.

    2) If You Can Write The Code, Please Let Me Know ! “I will Be Paying For The Code.” & That’s How I Can Appreciate Your Hard Work Through PayPal.

    Hope! You will seriously Look Into This Matter.

    #17452
    Avatar
    Suman Dahal

    How To Get Quick Support ?? I mean If I need to buy Add-On Or Something Like I need to have minimum token or what ? Please any one suggest me If I Have to Buy Any Add-on then I will Go for it !

    Thank You !

    #17457
    Avatar
    WPExperts

    @Suman There is no quick support. I am alone here supporting and developing myCRED. I can only do so much. Issue resolution is prioritised over customization requests. I am right now fully booked and can not provide any customization work, especially for third-party plugins that I do not know well enough.

    #17498
    Avatar
    Suman Dahal

    @Gabriel, I Can validate Field One with the above code but u simply modify above code so that I can validate More than one one field. For example, If I want to validate Field 1,3 & 6.

    #17499
    Avatar
    Suman Dahal

    *Could You

    #48367
    Avatar
    Ogunsipe Oluwaseun Isaac

    Hi, please I need this same issue to be resolved also but I’ve been trying to get familiar with the gravityform before submission hook but I couldn’t find a reliable solution yet!

    Gravityform has an hook to stop a form from not been submitted. The problem now is how do we get this done, I’m currently using a snippets to deduct balance due to the form selected value but I need it to be checking balance if sufficient before submission.

    THANKS IN advance

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

You must be logged in to reply to this topic.

11