Support » Plugin: Age Gate » How to use data inputted in to Age Gate

  • Resolved joshguss

    (@joshguss)


    Hello-

    I am wondering if there is a way to tap in to and use the data that a user inputs in to Age Gate. We have two scenarios we are looking at:

    1) Grab the birthdate that the user has inputted in to Age Gate and attach that as custom metadata to a WooCommerce order

    2) Using a custom form field (using the post_age_gate_custom_fields hook) for a user to select a state in the Age Gate form and using that state selection to do something based on the selection (i.e. conditionally display WooCommerce products based on eligibility to be shipped to the users state).

    Thanks!

Viewing 10 replies - 1 through 10 (of 10 total)
  • One more thing I would like to add as I have been working on the addition of the custom form field for users to in a state, and I kept receiving Fatal error: Uncaught Exception: Rule validation_rule does not have an error message. Initially I was trying this with the contains_list validation rule but then started testing generally with some other rules valid_email, integer, etc. just to see if it would return an error message and they were all returning fatal PHP errors due to no error message. The only one that seemed to be working for me was the required validation rule.

    Plugin Author Phil

    (@philsbury)

    Hi @joshguss,

    That fatal error is saying there’s not error message set for a given validation rule. There’s an example in the docs for adding totally custom validators and messages.

    BUT we can probably save you a load of time and effort here.
    Going back to your original post, both these are possible.

    For Adding the states, have a look at this extension (download at the bottom of the page) that adds some more options to Age Gate for some regional controls in including US states.

    Then you can get that info using the age_gate_form_success action:

    
    add_action('age_gate_form_success', 'ag_success', 10, 2);
    
    function ag_success($data, $errors = [])
    {
        error_log(print_r($data, 1));
    }
    

    The $data param will contain something like this:

    
    Array
    (
        [ag-region] => us
        [age_gate_age] => 21 // this is the required age, not users age
        [age_gate_content] => /
        [age_gate_d] => 00
        [age_gate_m] => 00
        [age_gate_y] => 0000
    )
    

    You can then do whatever you need with that info.

    Hope that all makes sense and helps!

    Thanks
    Phil

    Hey Phil,

    Thanks for sharing! I seem to be having an issue with setting specific regions as forbidden. The steps I have taken were to bulk add US States and then set one of the state’s restriction age to -1. In testing, regardless of the state selected (either one set to -1 OR one set to 21), I am able to access the site (assuming a birthdate that is entered is over 21).

    In regards to getting the info, I am having a hard time calling the ag_success function to access the array of inputted data.

    Thanks again!

    Plugin Author Phil

    (@philsbury)

    Hi @joshguss,

    Right, the first issue is that I’m stupid and managed to not trigger the functionality properly in the Regions extension and was only doing it the JS mode. I’ve fixed that and you should be able to update it to 1.0.4 to get that working.

    For the second part, what’s happening exactly?

    Cheers
    Phil

    Hey @philsbury,

    I am still having issues with the first issue. I tried completely deleting the plugin the re-installing the plugin, replacing the existing regions, and deleting existing regions and adding as new with -1 as the Restriction age but still no dice on my end.

    In regards to the second issue, i’m sure its just user error on my end. To test i’m just doing var_dump( ag_success($data) ); and its returning null.

    Plugin Author Phil

    (@philsbury)

    Hi @joshguss,

    Yeah that looks a bit off. Are you inside the hook?

    Something like:

    
    add_action('age_gate_form_success', 'ag_success', 10, 2);
    

    Then you need a function:

    
    function ag_success($data, $errors = [])
    {
        var_dump($data);
        wp_die(); // the die is just here otherwise it probably won't show on screen
    }
    

    For the regions, can you just confirm you’re on 1.0.4 of that? If you have a link up somewhere I can have a check of that one.

    Thanks
    Phil

    Ok. So the provided code worked in that it allowed me to see array of data being captured. So I guess what I am having an issue is how to go about using it. For example, trying to do something like:

    
    $ag_region = ag_success($data['ag-region']);
    if ( $ag_region == 'ny' ) {
      // do something
    } else {
      // do something else
    }
    

    In terms of the extension, I can confirm I am using version 1.0.4 (see screenshot HERE). I am currently developing locally so don’t yet have a URL to share… which I realize makes this more challenging to debug :/

    Plugin Author Phil

    (@philsbury)

    Hi @joshguss,

    I’ve had a quick look at 1.0.4. On my local it’s fine, on my test server I get a console error saying:

    Uncaught TypeError: Cannot set property 'textContent' of null

    Is that what you get? Either way, I think I know why so will have a look at it.

    As for your data, I get what you’re trying to do.

    You’ll need a little bit more though – one function to store the data and another one to retrieve it for later use, there’s a few ways to do it, but here’s an example using cookies:

    First, we hook the success action and save the data (doing a bit of processing too):

    
    add_action('age_gate_form_success', 'ag_success', 10, 2);
    
    function ag_success($data, $errors = [])
    {
        $dob = intval($data['age_gate_y']). '-' . str_pad(intval($data['age_gate_m']), 2, 0, STR_PAD_LEFT) . '-' . str_pad(intval($data['age_gate_d']), 2, 0, STR_PAD_LEFT);
    
        $from = new DateTime($dob);
        $to   = new DateTime('today');
        $age    = $from->diff($to)->y;
    
        $cookieData = json_encode([
            'age' => $age,
            'region' => $data['ag-region'] ?? false
        ]);
    
        setcookie('ag_data', $cookieData, 0, '/');
    }
    

    Here we calculate the age and save that rather than the full date of birth, but adjust to your needs, other than that, you shouldn’t need to call or do anything with this one.

    Then we need another function to get that data and use it:

    
    function ag_user_data()
    {
        $cookie = isset($_COOKIE['ag_data']) ? stripslashes($_COOKIE['ag_data']) : json_encode(['age' => false, 'region' => false]);
        $data = json_decode($cookie);
        return $data;
    }
    
    

    Here we just grab that data, or build and empty version if it doesn’t exist.

    Then as your example above you can do:

    
    if (ag_user_data()->region === 'ny') {
        // New York stuff
    } else {
        // Other stuff
    }
    

    Similarly, you could test and age here:

    
    if (ag_user_data()->age >= 21) {
        // Over 21
    } else {
        // Under 21
    }
    

    Hey @philsbury,

    This is great and works like a charm! Thank you 🙂

    I saw the update the extension before seeing this response so I did not get a chance to check the console before updating but after updating to 1.0.5 the restricted region is also now working.

    Thanks so much for you help with this.

    Plugin Author Phil

    (@philsbury)

    Hi @joshguss,

    Great stuff, I’ll close this down if every it’s all good.

    Thanks
    Phil

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.