• Resolved angelamd59

    (@angelamd59)


    I accidentally closed off the previous query I raised for this issue. The code I was given made no difference to the submitted invoice form. It is still not putting in the sequential number and is displaying AUTOINCREMENT_INT. Do you know why it suddenly stopped working in early June – was there an update to Forminator? This is getting quite urgent now. Thank you for your help.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @angelamd59

    I hope you’re well today!

    Yes, there has been a change in Forminator recently. We had to implement some additional security measures and one of them affects hidden fields which now can only store the value that actually is defined in the field – they are sanitized before saving to check if the actual value of the field matches the set option.

    However, there’s another code that actually works after those changes. It’s quite more complex so needs a bit of configuration (everything is explained in the code itself) but it also is way more “flexible” in terms of the number format.

    Please feel free to try it out:

    https://gist.github.com/adczk/fbf06b6abf7753df6b37234b13041e79

    You would need to add it the same way as previous code (note: not in addition to it but instead it) and in these lines you’d need to set configuration of the numbers as explained in code comments:

    private $macro         = '{formatted_value}'; // The text to replace in the Thank you message (fixed by AMIT)
    			private $field_name    = 'hidden-1'; // id of the field to hold autoincrement value; must be existing field of type hidden
    			private $form_ids      = array( 16699 ); // comma separated list of forms to use this with
    			private $start_number  = 1; // starting number to count up from
    			private $prefix        = 'CT2021_'; // field value prefix
    			private $suffix        = ''; // field value suffix
    			private $leading_zeros = 2; // how many leading zeros;

    A note on usage:

    1. If you want to include generated number in e-mail notifications:

    a) if you are using all_fields or all_non_empty_fields tags in message, it would work out of the box
    b) but if you are using individual fields tags, you will need to use whatever tag you defined in $macro line instead of actual hidden field ID
    c) point “b” also applies to mail subject – if you want to put number there

    2. hidden field in this case doesn’t have to include AUTOINCREMENT_INT, it can be set to anything – it doesn’t matter

    Best regards,
    Adam

    Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @angelamd59,

    I’m replying here to your recent question posted in this ticket regarding the above mu-plugin suggested by Adam.

    In order to test the snippet with your form, just make sure to change the number “16699” on line 55 to your form ID, which you can copy from the Forminator shortcode:

    private $form_ids = array( 16699 ); // comma separated list of forms to use this with

    – after this change your invoice IDs in PDF should look like: CT2021_001, CT2021_002, CT2021_003, and so on.

    Let’s say for example, if you don’t need prefixes or zeroes, and would like to leave only numbers like 1, 2, 3, ..., – then you’ll have to change these lines:

    			private $prefix        = 'CT2021_'; // field value prefix
    			private $suffix        = ''; // field value suffix
    			private $leading_zeros = 2; // how many leading zeros;

    to:

    			private $prefix        = ''; // field value prefix
    			private $suffix        = ''; // field value suffix
    			private $leading_zeros = 0; // how many leading zeros;

    The snippet should work fine with E2PDF, just make sure to reference the right field in your E2PDF template, for instance: {hidden-1}:

    https://prnt.sc/3atLEKri3wlz

    In case you’d like to connect a new form with your existing E2PDF template – click “Options” on the right, and select the correct form there under “Item”:

    https://prnt.sc/TkkPZ_H7Z6US

    I hope this helps. Let us know if you have any questions!

    Best Regards,
    Dmytro

    Thread Starter angelamd59

    (@angelamd59)

    Hi Dmytro, Help – that didn’t work for me at all – in fact I lost the ability to select the Order Form in E2PDF. So I put back the previous php file and at least the form was there for selecting. It was there before I tried uploading the latest php file. Also it was referencing the {hidden-1}, so something is not working. This is what I put in, going by your suggestions for the latest php

    <?php
    /**
     * Plugin Name: [Forminator] - Auto-increase field value (with prefix/suffix)
     * Description: [Forminator] - Auto-increase field value (with prefix/suffix)
     * Author: Adam @ WPMUDEV
     * Author URI: https://wpmudev.com
     * Based on: https://gist.github.com/wpmudev-sls/b607c860225b0b6bde502bcdec0184fa
     * License: GPLv2 or later
     *
     * Tested with Forminator 1.15.2 - 1.24.1 (backwards/future compatibility unknown)
     */
    
    
    /**
     * Update Jun 14th, 2023: now it also supports individual field in mail notifiation and adding number to e-mail subject
     *
     * if you use {all_fields} or {all_non_empty_fields} in e-mail notifications, generated value will be correctly used there
     * if you want to use individual fields macros in e-mail, you need to use macro 
     * defined in $macro in config below in message body instead of the hidden field macro
     * if you want to add generated number to mail notification subject, also use that defined $macro there
     *
     * for example, if $macro = '{formatted_value}' and $field_name='{hidden-1}'
     *
     * in subject and in e-mail notification use {formatted_value} instead of {hidden-1}
     *
    */
    
    
    /**
     * To reset autincrement number of a form, please login the admin
     * and then try to access this url: yoursite.com/wp-admin?wpmudev-fm-reset-number-by-form-id=[form_id]
     */
    
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    } elseif ( defined( 'WP_CLI' ) && WP_CLI ) {
    	return;
    }
    
    
    
    add_action( 'after_setup_theme', 'wpmudev_forminator_autoincreased_field', 100 );
    
    function wpmudev_forminator_autoincreased_field() {
    	if ( class_exists( 'Forminator' ) ) {
    
    		class WPMUDEV_FM_Autoincreased_field {
    
    			// CONFIGURATION ###############
    
    			
    			private $macro         = '{formatted_value}'; // The text to replace in the Thank you message (fixed by AMIT)
    			private $field_name    = 'hidden-1'; // id of the field to hold autoincrement value; must be existing field of type hidden
    			private $form_ids      = array( forminator_form id="257" ); // comma separated list of forms to use this with
    			private $start_number  = 1; // starting number to count up from
    			private $prefix        = 'B_'; // field value prefix
    			private $suffix        = ''; // field value suffix
    			private $leading_zeros = 0; // how many leading zeros;
    			// 0: number are like 1, 2, 3, 4, 5 and so on
    			// 1: number are like 01, 02, 03...10, 11 and so on
    			// 2: numbers are like 001, 002...011, 012, 101, 520 and so on
    			// 3: numbers are like 0001, 002... 0011... 0520, 1234 and so on...
    			// ...
    
    			// DO NOT EDIT BELOW ######################
    
    			private $entry;
    			// AMIT: Variable to hold formatted value among filters
    			private $formatted_value;
    
    			public function __construct() {
    
    				// reset count (for admin)
    				add_action( 'admin_init', array( $this, 'reset_number' ) );
    
    				add_filter( 'forminator_custom_form_submit_field_data', array( $this, 'set_custom_value' ), 10, 2 );
    
    				add_filter( 'forminator_custom_form_mail_data', array( $this, 'wpmudev_fm_mail_form_data' ), 10, 3 );
    				
    				// handle $macro in notification subject and message 
    				add_filter( 'forminator_custom_form_mail_admin_subject', array( $this, 'wpmudev_fm_mail_macro_replace' ), 10, 3 );
    				add_filter( 'forminator_custom_form_mail_admin_message', array( $this, 'wpmudev_fm_mail_macro_replace' ), 10, 3 );
    
    				// AMIT: Update Formatted value macro in the Thank you response
    				add_filter( 'forminator_form_ajax_submit_response', array( $this, 'inject_formatted_value_in_response' ), 20 );
    				add_filter( 'forminator_form_submit_response', array( $this, 'inject_formatted_value_in_response' ), 20 );				
    			}
    
    			// helper - format value with leading zeros, prefix and suffix
    			public function do_format_value( $value ) {
    
    				$formatted_value = $value;
    
    				// set leading zeros
    				$add_zeros = ( $this->leading_zeros + 1 ) - strlen( $value );
    
    				if ( $add_zeros > 0 ) {
    
    					for ( $i = 1; $i <= $add_zeros; $i++ ) {
    						$formatted_value = '0' . $formatted_value;
    					}
    				}
    
    				// add prefix/suffix
    				$formatted_value = $this->prefix . $formatted_value . $this->suffix;
    
    				return $formatted_value;
    
    			}
    
    			// helper - get clean number by removing prefix, suffix and leading zeros
    			//
    			// NOT USED
    			//
    			// but shows how to "unformat"/"decode" formatted value to a raw number
    			public function do_unformat_value( $value ) {
    
    				$unformatted_value = (int) ltrim( str_replace( $this->suffix, '', str_replace( $this->prefix, '', $value ) ), '0' );
    
    				return $unformatted_value;
    
    			}
    
    			// helper - get number from DB option
    			public function get_number( $form_id ) {
    
    				static $raw_number;
    
    				if ( ! $raw_numer ) {
    
    					$formatted_numbers = get_option( 'wpmudev_fm_autoincremented_field', array() );
    
    					if ( isset( $formatted_numbers[ $form_id ] ) ) {
    						$raw_number = $formatted_numbers[ $form_id ];
    					} else {
    						$raw_number = $this->start_number;
    					}
    				}
    
    				return $raw_number;
    
    			}
    
    			// reset number function
    			public function reset_number() {
    				if ( current_user_can( 'manage_options' ) && isset( $_GET['wpmudev-fm-reset-number-by-form-id'] ) ) {
    					$form_id = (int) $_GET['wpmudev-fm-reset-number-by-form-id'];
    					if ( $form_id ) {
    
    						$order_numbers             = get_option( 'wpmudev_fm_autoincremented_field', array() );
    						$order_numbers[ $form_id ] = $this->start_number;
    
    						update_option( 'wpmudev_fm_autoincremented_field', $order_numbers );
    
    					}
    				}
    			}
    
    			// get, increase and update value
    			public function set_custom_value( $field_data_array, $form_id ) {
    
    				if ( ! in_array( $form_id, $this->form_ids ) ) {
    					return $field_data_array;
    				}
    
    				$current_value = $this->get_number( $form_id );
    				// AMIT: Save Formatted value in class level variable
    				$this->formatted_value = $this->do_format_value( $current_value );
    
    				$custom_data_array[] = array(
    					'name'  => $this->field_name,
    					'value' => $this->formatted_value, // AMIT, changed
    				);
    
    				// update form submission data
    				$field_data_array = array_merge( $field_data_array, $custom_data_array );
    
    				// increase value for next submission
    				$current_value++;
    				$order_numbers[ $form_id ] = $current_value;
    				// and remember it in DB option
    				update_option( 'wpmudev_fm_autoincremented_field', $order_numbers );
    
    				return $field_data_array;
    
    			}
    
    			public function wpmudev_fm_mail_form_data( $data, $custom_form, $entry ) {
    				
    				/* make sure that field variable in notification uses saved data instead of submitted */
    				$data[ $this->field_name ] = $entry->meta_data[ $this->field_name ]['value'];
    				return $data;
    
    			}
    			
    			public function wpmudev_fm_mail_macro_replace( $message, $custom_form, $data ) {
    				
    				$message = str_replace( $this->macro, $this->formatted_value, $message );
    				
    				return $message;
    				
    			}
    
    			/*
    			** AMIT: Inject the formatted value in thank you response
    			*/
    			public function inject_formatted_value_in_response( $response ) {
    				if ( isset( $this->formatted_value ) ) {
    					$response['message'] = str_replace( $this->macro, $this->formatted_value, $response['message'] );
    				}
    
    				return $response;
    			}
    		}
    
    		$run = new WPMUDEV_FM_Autoincreased_field();
    
    	}
    }

    I have tried the previous php code, and it SEEMS to be working. I will let you know when it is confirmed. Thanks.

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @angelamd59

    I have tried the previous php code, and it SEEMS to be working. I will let you know when it is confirmed. Thanks.

    Just to make sure we are in the same page, everything is working well now?

    Best Regards
    Patrick Freitas

    Thread Starter angelamd59

    (@angelamd59)

    Hi Patrick,

    Yes thank you. Everyone is happy. I will close this post.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Sequential numbering on the Forminator form submission has stopped workin’ is closed to new replies.