Random or sequential number generator
-
Hello, since I’m using Forminator to create a ticket sell form, I’d like to give each buyer a random or sequential code to be sent in the notification email so that it is unique for each buyer and, in the check-in phase, I can ask for this unique number to identify the ticket buyers.
It’s possible to do that with Forminator?
BTW I think this one is the best form builder for WP! 🙂
-
I think that the sequential number should be good instead of a random one. I’ve seen a randonìm number generator snippet somwhere here but the post was a little old.
Well I’ve discovered that using {submission-id} in the notification mail do the trick.
Now the question is only how can I output this number in the Google Sheet connecting the form through the integration.I’ve seen that I can select this field in the notification email but how can I also ouput this field in the connected Google Sheet?
If I can do that I’ve done what I need 🙂
Hi @shiftsrl
I hope you are doing well.
I am afraid there is no way to send the submission ID to Google sheet but you can use this code: https://gist.github.com/patrickfreitasdev/96e3bdf3bbbc25ca6f142855c866859f by replacing a hidden field, to use it, please use as a mu-plugin https://wpmudev.com/blog/wordpress-mu-plugins/
Another code that can be helpful:
<?php if ( ! class_exists( 'WPMUDEV_Forminator_Random_Number_Generator' ) ) { class WPMUDEV_Forminator_Random_Number_Generator { private $length = 5; private $form_id = array(2910, 2878, 1633); private $form_field = 'hidden-1'; private $email_macro = '{random_number}'; private static $_instance = null; private $generations = 0; private $random_number = ''; private $current_form = ''; public static function get_instance() { if ( is_null( self::$_instance ) ) { self::$_instance = new WPMUDEV_Forminator_Random_Number_Generator(); } return self::$_instance; } private function __construct() { $this->init(); } public function init() { add_filter( 'forminator_custom_form_submit_field_data', array( $this, 'wpmudev_randomize_hidden_input' ), 10, 2 ); add_filter( 'forminator_replace_variables', array( $this, 'forminator_replace_variables' ), 10, 2 ); } public function forminator_replace_variables( $content, $content_before_replacement ) { return str_replace( $this->email_macro, $this->random_number, $content ); } public function wpmudev_randomize_hidden_input( $field_data_array, $form_id ) { foreach ($this->form_id as $form_ids){ if ( (int) $form_id == (int) $form_ids ) { $this->current_form = $form_id; $this->random_number = $this->wpmudev_randomizer(); $incremental = get_post_meta( $form_ids, 'autoincrement_int', true ); if ( ! $incremental ) { $incremental = 1; } else { $incremental = $incremental + 1; } update_post_meta( $form_ids, 'autoincrement_int', $incremental ); $field_data_array[] = array( 'name' => $this->form_field, 'value' => $this->random_number, ); } } return $field_data_array; } public function wpmudev_is_unique( $num = '' ) { global $wpdb; $query = "SELECT COUNT(*) FROM {$wpdb->prefix}frmt_form_entry_meta WHERE meta_key LIKE '{$this->form_field}' AND meta_value LIKE '{$num}' AND entry_id IN( SELECT entry_id FROM {$wpdb->prefix}frmt_form_entry WHERE form_id IN (" . implode(',', $this->form_id) . ") );"; if ( 0 === (int) $wpdb->get_var( $query ) ) { return true; } else { $this->generations++; return false; } } public function wpmudev_randomizer() { $num = ''; $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; for ( $i = 0; $i < $this->length; $i++ ) { $num .= $characters[ rand( 0, strlen( $characters ) - 1 ) ]; } if ( $this->generations > pow( strlen( $characters ), $this->length ) ) { return null; } if ( empty( $num ) ) { return null; } $rand_num = get_post_meta( $this->current_form, 'autoincrement_int', true ); if ( ! $rand_num ) { $rand_num = 1; } else { $rand_num = $rand_num + 1; } if ( $this->wpmudev_is_unique( '#'.$num.$rand_num ) ) { return '#'.$num.$rand_num; } else { return $this->wpmudev_randomizer(); } } } if ( ! function_exists( 'wpmudev_forminator_random_number_generator' ) ) { function wpmudev_forminator_random_number_generator() { return WPMUDEV_Forminator_Random_Number_Generator::get_instance(); }; add_action( 'plugins_loaded', 'wpmudev_forminator_random_number_generator', 10 ); } }It will create random strings adding to hidden-1 and you can also use the {random_number} macro on emails.
Similar to previous one, you can use it as a mu-plugin, just set your form ID in
private $form_id = array(2910, 2878, 1633);Best Regards
Patrick FreitasHello Patrick, it seems to work but the notification email to the admin and to the user are not sent. The data are in the Google Sheet and it’s ok but we need to fix the email problem.
Hello again. It was my fault, everything work as expected. You’re the best and your support is fantastic!
Hi @shiftsrl
Thanks for update. I’m glad it’s working for you now!
I understand that we can consider the issue resolved now so I’m marking this ticket as such but if you have any follow up questions, please don’t hesitate to ask.
Best regards,
AdamHello, i tried this code from patrick :
<?php if ( ! class_exists( 'WPMUDEV_Forminator_Random_Number_Generator' ) ) { class WPMUDEV_Forminator_Random_Number_Generator { private $length = 5; private $form_id = array(2910, 2878, 1633); private $form_field = 'hidden-1'; private $email_macro = '{random_number}'; private static $_instance = null; private $generations = 0; private $random_number = ''; private $current_form = ''; public static function get_instance() { if ( is_null( self::$_instance ) ) { self::$_instance = new WPMUDEV_Forminator_Random_Number_Generator(); } return self::$_instance; } private function __construct() { $this->init(); } public function init() { add_filter( 'forminator_custom_form_submit_field_data', array( $this, 'wpmudev_randomize_hidden_input' ), 10, 2 ); add_filter( 'forminator_replace_variables', array( $this, 'forminator_replace_variables' ), 10, 2 ); } public function forminator_replace_variables( $content, $content_before_replacement ) { return str_replace( $this->email_macro, $this->random_number, $content ); } public function wpmudev_randomize_hidden_input( $field_data_array, $form_id ) { foreach ($this->form_id as $form_ids){ if ( (int) $form_id == (int) $form_ids ) { $this->current_form = $form_id; $this->random_number = $this->wpmudev_randomizer(); $incremental = get_post_meta( $form_ids, 'autoincrement_int', true ); if ( ! $incremental ) { $incremental = 1; } else { $incremental = $incremental + 1; } update_post_meta( $form_ids, 'autoincrement_int', $incremental ); $field_data_array[] = array( 'name' => $this->form_field, 'value' => $this->random_number, ); } } return $field_data_array; } public function wpmudev_is_unique( $num = '' ) { global $wpdb; $query = "SELECT COUNT(*) FROM {$wpdb->prefix}frmt_form_entry_meta WHERE meta_key LIKE '{$this->form_field}' AND meta_value LIKE '{$num}' AND entry_id IN( SELECT entry_id FROM {$wpdb->prefix}frmt_form_entry WHERE form_id IN (" . implode(',', $this->form_id) . ") );"; if ( 0 === (int) $wpdb->get_var( $query ) ) { return true; } else { $this->generations++; return false; } } public function wpmudev_randomizer() { $num = ''; $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; for ( $i = 0; $i < $this->length; $i++ ) { $num .= $characters[ rand( 0, strlen( $characters ) - 1 ) ]; } if ( $this->generations > pow( strlen( $characters ), $this->length ) ) { return null; } if ( empty( $num ) ) { return null; } $rand_num = get_post_meta( $this->current_form, 'autoincrement_int', true ); if ( ! $rand_num ) { $rand_num = 1; } else { $rand_num = $rand_num + 1; } if ( $this->wpmudev_is_unique( '#'.$num.$rand_num ) ) { return '#'.$num.$rand_num; } else { return $this->wpmudev_randomizer(); } } } if ( ! function_exists( 'wpmudev_forminator_random_number_generator' ) ) { function wpmudev_forminator_random_number_generator() { return WPMUDEV_Forminator_Random_Number_Generator::get_instance(); }; add_action( 'plugins_loaded', 'wpmudev_forminator_random_number_generator', 10 ); } }Its working great, i changed length to 6 and i have results in mail like expected.
Result is in this form : #MJWSAY2
Problem is that i would need to have the same result but only numbers instead of letters and without #, because i want to put this {random_number} in pre-generated link for online payment, but it only accept numbers, and i’m not experienced enough to modify this part of the code.
Thanks by advance if you can do anything to this.
-
This reply was modified 3 years, 1 month ago by
squalljustice.
I understand that the issue is related to what was discussed here but, as per this forum rules
please start your own separate ticket.
We’ll assist you there.
Kind regards,
Adam -
This reply was modified 3 years, 1 month ago by
The topic ‘Random or sequential number generator’ is closed to new replies.