• Resolved brucebarbour

    (@brucebarbour)


    Hi there. I need to be able to issue enquirers with a reference number that begins “ZZZ”. I’ve found a piece of code that enables me to do that and it’s working fine. My question is, when the reference number gets up to ZZZ999, will the next one be ZZZ000, or ZZZ1000?

    Kind regards,
    Bruce

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

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @brucebarbour

    I hope you are doing good today.

    Tricky question ?? I pinged our Forminantor Team to review your query. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @brucebarbour

    I hope you are doing well.

    Could you please share the code that you are using and we can send it to our developers?

    Best Regards
    Patrick Freitas

    Thread Starter brucebarbour

    (@brucebarbour)

    Hi Patrick. Doing fine thanks. I hope you are too. Please see the code below as requested.

    <?php
    /**
     * Plugin Name: [Forminator] - Custom submission id as order number
     * Description: [Forminator] - Custom submission id as order number
     * Jira: SLS-224
     * Author: Thobk @ WPMUDEV
     * Author URI: https://premium.wpmudev.org
     * License: GPLv2 or later
     */
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    } elseif ( defined( 'WP_CLI' ) && WP_CLI ) {
    	return;
    }
    
    add_action( 'after_setup_theme', 'wpmudev_forminator_custom_submission_number_for_custom_form_func', 100 );
    
    /**
     * To reset order number of a form, please login the admin
     * and then try to access this url: yoursite.com/wp-admin?wpmudev-fm-reset-order-number-by-form-id=[form_id]
     */
    
    function wpmudev_forminator_custom_submission_number_for_custom_form_func() {
    	if ( class_exists( 'Forminator' )  ) {
    
    		class WPMUDEV_FM_Order_Number{
    			private $include_form_ids = [];
    			private $exclude_form_ids = [];//enter list exclude form ids, e.g: [234,456]
    			private $start_number = ZZZ999;
    
     			private $entry;
    
    			public function __construct(){
    
    				add_filter( 'forminator_replace_custom_form_data', array( $this, 'custom_order_number' ), 10, 4 );
    				add_action( 'forminator_custom_form_mail_after_send_mail', array( $this, 'increase_order_number' ), 10, 1 );
    
    				add_action( 'admin_init', array( $this, 'reset_order_number' ) );
    
    				// change number order for admin:
    				add_filter( 'forminator_custom_form_entries_iterator', array( $this, 'custom_order_number_for_admin' ), 10, 2 );
    			}
    
    			public function reset_order_number(){
    				if( current_user_can( 'manage_options' ) && isset( $_GET['wpmudev-fm-reset-order-number-by-form-id']) ){
    					$form_id = (int) $_GET['wpmudev-fm-reset-order-number-by-form-id'];
    					if( $form_id ) {
    						$order_numbers = get_option( 'wpmudev_fm_custom_submission_id', array() );
    						if( isset( $order_numbers[ $form_id ] ) ){
    							unset( $order_numbers[ $form_id ] );
    							update_option( 'wpmudev_fm_custom_submission_id', $order_numbers );
    						}
    					}
    				}
    			}
    
    			public function custom_order_number( $content, $custom_form, $data, $entry ){
    
    				// maybe exclude the form.
    				if( ! empty( $this->include_form_ids ) ){
    					if( ! in_array( $custom_form->id, $this->include_form_ids ) ){
    						return $content;
    					}
    				}elseif( ! empty( $this->exclude_form_ids ) && in_array( $custom_form->id, $this->exclude_form_ids ) ){
    					return $content;
    				}
    
    				if( version_compare( FORMINATOR_VERSION, '1.3.1', '>' ) ){
    					$submission_id = '#'. esc_html( $entry->entry_id );// add prefix # to avoid the conflict.
    					$replace = '#'. $this->get_number_order( $entry->form_id );
    				}else{
    					$submission_id = esc_html( $entry->form_id . $entry->entry_id );
    					$replace = $this->get_number_order( $entry->form_id );
    				}
    
    				if( false !== strpos( $content, $submission_id ) ){
    					$this->entry = $entry;
    					$content = str_replace( $submission_id, $replace, $content );
    				}
    
    				return $content;
    			}
    
    			public function increase_order_number(){
    				if( $this->entry ){
    					$order_number = $this->get_number_order( $this->entry->form_id );
    					// set to entry meta
    					$this->entry->set_fields(
    						array( 
    							array(
    								'name'=> 'order_number',
    								'value' => $order_number
    							),
    						)
    					);
    
    					$order_number ++;
    					$order_numbers = get_option( 'wpmudev_fm_custom_submission_id', array() );
    					$order_numbers[ $this->entry->form_id ] = $order_number;
    					update_option( 'wpmudev_fm_custom_submission_id', $order_numbers );
    
    					// reset entry
    					$this->entry = null;
    				}
    			}
    
    			public function get_number_order( $form_id ){
    				static $order_number;
    				if( ! $order_number ){
    					$order_numbers = get_option( 'wpmudev_fm_custom_submission_id', array() );
    					if( isset( $order_numbers[ $form_id ] ) ){
    						$order_number = $order_numbers[ $form_id ];
    					}else{
    						$order_number = $this->start_number;
    					}
    				}
    
    				return $order_number;
    			}
    
    			public function custom_order_number_for_admin( $iterator, $entry ){
    				// maybe exclude the form.
    				if( ! empty( $this->include_form_ids ) ){
    					if( ! in_array( $entry->form_id, $this->include_form_ids ) ){
    						return $iterator;
    					}
    				}elseif( ! empty( $this->exclude_form_ids ) && in_array( $entry->form_id, $this->exclude_form_ids ) ){
    					return $iterator;
    				}
    
    				$order_number = $entry->get_meta('order_number');
    				if( $order_number ){
    					$iterator['entry_id'] .= '-'. $order_number;
    					$iterator['detail']['items']  = array( array_shift( $iterator['detail']['items'] ), array(
    						'type'        => 'number',
    						'label'       => 'Order',
    						'value'       => '#'. $order_number,
    						'sub_entries' => array(),
    					) ) + $iterator['detail']['items'];
    				}
    
    				return $iterator;
    			}
    
    		}
    
    		$run = new WPMUDEV_FM_Order_Number;
    	}
    }
    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @brucebarbour

    Thank you for the code, we sent to our developers to verify it.

    Best Regards
    Patrick Freitas

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @brucebarbour

    Could you please make the following changes?

    Revert the code to the original version,
    https://gist.github.com/wpmudev-sls/b607c860225b0b6bde502bcdec0184fa

    Keep the start number untouched and then just update the prefix in:

    if( version_compare( FORMINATOR_VERSION, '1.3.1', '>' ) ){
    	$submission_id = '#'. esc_html( $entry->entry_id );// add prefix # to avoid the conflict.
    	$replace = '#'. $this->get_number_order( $entry->form_id );
    }else{
    	$submission_id = esc_html( $entry->form_id . $entry->entry_id );
    	$replace = $this->get_number_order( $entry->form_id );
    }

    To

    if( version_compare( FORMINATOR_VERSION, '1.3.1', '>' ) ){
    	$submission_id = '#'. esc_html( $entry->entry_id );// add prefix # to avoid the conflict.
    	$replace = '#ZZZ'. $this->get_number_order( $entry->form_id );
    }else{
    	$submission_id = esc_html( $entry->form_id . $entry->entry_id );
    	$replace = 'ZZZ'. $this->get_number_order( $entry->form_id );
    }

    Best Regards
    Patrick Freitas

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @brucebarbour

    I hope you are doing well and safe!

    We haven’t heard from you in a while, I’ll mark this thread as resolved.

    Feel free to let us know if you have any additional question or problem.

    Best Regards
    Patrick Freitas

    Hi
    How do I use this code in my form? Do in need to add any field to my form? Also will the number pushed to google sheets.

    Thank You

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @basilj

    You can add the code as a mu-plugin file, to find more about it:
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    In case you still have trouble adding the code, could you create a new thread so we don’t spam the thread starter?

    Best Regards
    Patrick Freitas

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Forminator submission ID’ is closed to new replies.