Gravity Forms Wordpress Date Picker Age Verification - wordpress

I am using Gravity Forms (1.8.9) and Wordpress (3.9.1)
I have a form field as a datepicker on my site, and I want to allow submissions only if the user is 21 or older to be able to submit the form.
I was originally using the following code. It only worked for a single form for a while, but then it stopped working completely: http://lanche86.com/gravity-forms-18-years-old-verification/
I would like to be able to use the same code on different forms. Help!

You can do something like this:
add_filter("gform_field_validation_1_1", "dob_validate", 10, 4);
function dob_validate($result, $value, $form, $field){
//Check if dob field matches required age
if ($result["is_valid"]){
// this the minimum age requirement we are validating
$minimum_age = 18;
// calculate age in years like a human, not a computer, based on the same birth date every year
$age = date('Y') - substr($value, 6, 4);
if (strtotime(date('Y-m-d')) - strtotime(date('Y') . '-' . substr($value, 0, 2) . '-' . substr($value, 3, 2)) < 0) {
$age--;
}
if( $age < $minimum_age ){
$result["is_valid"] = false;
$result["message"] = "Sorry, you must be at least $minimum_age years old. You're $age years old.";
}
}
//Check if dob field is empty
if(empty($value)){
$result["is_valid"] = false;
$result["message"] = "This field is required.";
}
return $result;
}
I'm using Gravity Forms 1.8.8 and latest Wordpress and works as desired. Screenshot:
You can also edit this according to form and field:
add_filter("gform_field_validation_1_1", "dob_validate", 10, 4);
Where gform_field_validation_1_1 is for form 1 and field 1. If your forms id is 8 and field number is 2, you can change it to gform_field_validation_8_2.
You can also add that same filter multiple times for multiple forms and fields without recreating the dob_validate function.

If anybody still needs to implement this, try this:
add_filter( 'gform_field_validation_1_1', function ( $result, $value, $form, $field ) {
if ( $result['is_valid'] ) {
if ( is_array( $value ) ) {
$value = array_values( $value );
}
$date_value = GFFormsModel::prepare_date( $field->dateFormat, $value );
$today = new DateTime();
$diff = $today->diff( new DateTime( $date_value ) );
$age = $diff->y;
if ( $age < 18 ) {
$result['is_valid'] = false;
$result['message'] = 'Underage';
}
}
return $result;
}, 10, 4 );
It's on Gravity Forms Documentation: https://docs.gravityforms.com/gform_field_validation/#13-date-field-age-validation

Related

How to get value from a field key (Ninja Forms)

Having a try with Ninja Forms, I’m actually able to get value from a field ID using $form_data array variable.
function my_ninja_function( $form_data ) {
$my_field_id = 1;
$my_value_from_field_id = $form_data['fields'][$my_field_id]['value'];
echo $my_value_from_field_id;
// output value is possible
}
And now trying to get value from a field key, without success...
$my_field_key = 'my_key';
$my_value_from_field_key = $form_data['fields'][$my_field_key]['value'];
echo $my_value_from_field_key;
// output value is not possible
with a little more effort...
$form_fields = $form_data['fields'];
foreach( $form_fields as $field ){
$field_value = $field['value'];
$field_key = $field['key'];
$data[$field_key] = $field_value;
};
$my_value_from_key = $data['my_key'];
echo $my_value_from_key;
// output is possible
It works!
By value I'm assuming you mean the field's label. You can get a field's label from the field's settings like this:
$form_id = 1;
$form_fields = Ninja_Forms()->form($form_id)->get_fields();
foreach( $form_fields as $field ) {
$model = $field->get_settings();
$label = $model['label'];
}
if you really do mean value, then perhaps you are referring to a form submission's field value. You can get those like this:
$sub_id = 1; // Need to know the submission's ID
$sub = Ninja_Forms()->form()->sub($sub_id)->get();
$form_id = 1;
$form_fields = Ninja_Forms()->form($form_id)->get_fields();
foreach( $form_fields as $field ) {
$model = $field->get_settings();
$value = $sub->get_field_value($model['key']); // User submitted value
}
Note that NinjaForms has added field keys in version 3 after I made the suggestion, as previous versions had no unique field identifier which made exporting/importing fields and forms very problematic.

Issue with Easy Booking plugin and Woocommerce

We are using the Easy Booking plugin for a client’s project and they want the ability to make the dates recurring. I.E they use the calendar to book Mon, Tues and Wed and want to have it recurring for the next 3 weeks.
We added some JS functionality, with a few form fields, that modifys the data-booking_price so that it is the price * recurring modifier. The order flow works fine while the quantity is one, but if we increase the quantity, the whole thing breaks apart. After we add the item to the cart and then go to View Cart, the price is set to the original booking price, sans the modifier.
You can view it in action here: http://bethpark.dev.ksand.com/product/meter-bags
My question is how does how does the plugin pass the total of the product along to the cart? From what I can see, it’s not updating Woo Sessions with the total that is getting generated. Is that not accurate? I realize we are modifying how the plugin inherenly works, but any sort of pointing in the right direction would be much appreciated!
-Anthony
I've updated WC()->cart->total on both the cart page and the checkout page, but it keeps getting overwritten. I've also manually set the total in WC()->sessions but that also get's overwritten (I've since commented these out since this felt extremely hacky.)
//function to detect if PHP session is started
function is_session_started()
{
if ( php_sapi_name() !== 'cli' ) {
if ( version_compare(phpversion(), '5.4.0', '>=') ) {
return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
} else {
return session_id() === '' ? FALSE : TRUE;
}
}
return FALSE;
}
add_action( 'woocommerce_before_add_to_cart_button' , 'bpa_custom_add_checkout_fields', 50, 0 );
function save_recursion(){
//if the user is getting the bags with recursive dates set, we will attempt to save them so that they can be used at a later time.
if( isset($_POST['recursive_weeks']) ){
$id = 0;
if( is_user_logged_in() ){
$id = get_current_user_id();
}
// if user is not logged in or if WP couldn't get user ID
if($id > 0){
update_user_meta($id, 'bpa_meter_bag_recursion', $_POST['recursive_weeks']);
} else {
if ( is_session_started() === FALSE ) session_start();
$_SESSION['bpa_meter_bag_recursion'] = $_POST['recursive_weeks'];
}
}
}
add_action('woocommerce_add_to_cart', 'save_recursion');
function custom_add_to_cart( $item_data, $cart_item) {
//this adds the custom recursion dates to the items data
//if this is the meter bag product
if($cart_item['product_id'] == '2476'){
//check if user is logged in, get recursion from user meta. if not try to get sessions
if( is_user_logged_in() ){
$id = get_current_user_id();
$recursion = get_user_meta($id, 'bpa_meter_bag_recursion');
$_POST['recursive_weeks'];
} else {
session_start();
$recursion = $_SESSION['bpa_meter_bag_recursion'] ;
}
// change the message to reflect that this is going to be multiple dates
$item_data[0]['name'] = "First Day of First Week";
$item_data[1]['name'] = "Last Day of First Week";
//get that first week dates in the correct format
$start_date = new DateTime($item_data[0]['value']);
$end_date = new DateTime($item_data[1]['value']);
$first_start_date = $start_date->format('Y-m-d');
$first_end_date =$end_date->format('Y-m-d');
//iterate over the recursive weeks and add these as products to the cart
$week = 1;
$quantity = $cart_item['quantity'];
for($i = 1; $i <= ($recursion[0] * 2) - 2; $i++ ){
if( ($i % 2) != 0){
$item_data[$i + 1]['name'] = "First Day of the Next Week ";
$startDate = strtotime( $first_start_date . " +" . $week ." week" );
$item_data[$i + 1]['value'] = date('F jS Y', $startDate);
} else {
$item_data[$i + 1]['name'] = "Last Day of the Next Week ";
$endDate = strtotime( $first_end_date . " +" . $week ." week");
$item_data[$i + 1]['value'] = date('F jS Y', $endDate);
$week++;
}
}
if ( is_session_started() === FALSE ) session_start();
$_SESSION['recursive_dates'] = serialize($item_data);
if( isset($_SESSION['recursive_dates']) ){
//var_dump($_SESSION['recursive_dates']);
}
return $item_data;
}
}
add_filter('woocommerce_get_item_data', 'custom_add_to_cart', 10, 2);
What I am expecting to have happen is that on the product screen, the updated product total would get added to the cart total, and viewable from the cart and checkout. This works until the quantity is increased. Not sure why this is an issue since I would presume that the total would be prod_total * quantity, but that doesn't appear to be how this works. I'm not sure if this is a woo thing or an Easy Bookings thing (I'm tending to lean towards a woo thing, but my hours of research hasn't found anything, or I'm googling the wrong stuff. )
Any help in pointing me in the right direction is mucho appreciated.

Force auto replace letters when typing inside order form

Does anyone have an idea on how to auto replace letters in woocommerce order forms! basically I need when someone is typing in greeklish to auto replace those letters to Greek!
I think you will have to use this hook to get the newly created order :
add_action( 'woocommerce_new_order', 'convert_greeklish_for_wc_order', 1, 1 );
function create_invoice_for_wc_order() {
function create_invoice_for_wc_order( $order_id ) {
// get order details data...
$order = new WC_Order( $order_id );
// Here goes the code to get all the fields
// Convert fields to greek
// Set new fields values
};
}
And adapt this code to convert greeklisk to greek:
<?php
function greeklish($new_text){
$greek_len = array('α','ά','Ά','Α','β','Β','γ', 'Γ', 'δ','Δ','ε','έ','Ε','Έ','ζ','Ζ','η','ή','Η','θ','Θ','ι','ί','ϊ','ΐ','Ι','Ί', 'κ','Κ','λ','Λ','μ','Μ','ν','Ν','ξ','Ξ','ο','ό','Ο','Ό','π','Π','ρ','Ρ','σ','ς', 'Σ','τ','Τ','υ','ύ','Υ','Ύ','φ','Φ','χ','Χ','ψ','Ψ','ω','ώ','Ω','Ώ',' ',"'","'",',');
$english_len = array('a', 'a','A','A','b','B','g','G','d','D','e','e','E','E','z','Z','i','i','I','th','Th', 'i','i','i','i','I','I','k','K','l','L','m','M','n','N','x','X','o','o','O','O','p','P' ,'r','R','s','s','S','t','T','u','u','Y','Y','f','F','ch','Ch','ps','Ps','o','o','O','O',' ','',' ',' ');
$new_text = str_replace($greek_len,$english_len,$new_text);
return $new_text;
}
$conv = greeklish("Το κείμενο σου εδώ!");
echo $conv; #To keimeno sou edo!
?>

Trouble with passing variable from one function to next?

I am trying to edit a plugin in wordpress that deals with a points system to suit my needs. I have two functions, A and B. Function A is basically telling the plugin to deal a different amount of points depending on a post term Id. Function B is now trying to incorporate the variable $amount from Function A to limit users from ever going into a negative balance. My problem is that I can't get Function B to get the $amount from Function A when determining its argument. I tried to set it as a global variable but it didn't work. Can someone suggest what would be the best approach for this?
In basic words, the plugin is set to always take 100 points from the user when
"dealing_points". However, if the post term_id is equal to any of the below, then the amount of points changes. Now I need the $amount in function B to always equal the new $amount that was calculated within the if condition when completing its argument.
function A( $run, $request ) {
if ( $run['ref'] == 'dealing_points' ) {
// Original amount (Usually take 100 points)
$amount = $request['amount'];
if(isset($_POST['term_id'])){
if ($_POST['term_id'] == '74' )
$amount = -50;
elseif ($_POST['term_id'] == '75' )
$amount = 50;
$run['amount'] = $amount;
}
}
return $run;
}
function B( $reply, $request) {
if ( $reply === false || $request['amount'] > 0 ) return $reply;
extract( $request );
$user_balance = get_users_balance( $user_id, $type );
// Don't allow user to ever go below zero
if ( ( $user_balance + $amount ) < 0 ) return null;
return $reply;
}

Display a rounded count of Custom Post Type

I'd like display a rounded up count of the total number of custom post types.
Currently I am using this
<?php
$count_posts = wp_count_posts('listing');
$published_posts = $count_posts->publish;
echo $published_posts;
?>
but would like to round the number to nearest hundred, so for example,
if the count was 120 it would show 100
if the count was 158 it would show 200
if the count was 1088 it would show 1000
Thanks
This question not related to WordPress, but you will get solution by follow
PHP Custom functions: add into functions.php
if( !function_exists('ceiling') )
{
function ceiling($number, $significance = 1)
{
return ( is_numeric($number) && is_numeric($significance) ) ? (round($number/$significance)*$significance) : false;
}
}
if( !function_exists('ceilNumber') )
{
function ceilNumber($number)
{
return ceiling($number, (int)'1'.str_repeat('0', strlen($number)-1));
}
}
Call above function into your code
$count_posts = wp_count_posts('listing');
$published_posts = $count_posts->publish;
echo ceilNumber($published_posts);

Resources