[Forminator Pro] Update user meta with amount of submissions for a form

Hello! Is it possible to update a user meta field called invoice_amount with the amount of submissions for a form a user has posted?

E.g. I have a form with ID 7446. Each time a user submits this form, their invoice_amount meta should get updated. It’ll be updated with 1 value first time, 5 on fifth time, and so forth. Must take only this form into account, none other.

Hope this makes sense!

  • Laura Zeballos
    • Staff

    HI Fatima Buzdar ,

    Hope this message finds you well and thanks for reaching us.

    Let me understand your request:

    1. You have a Forminator form that only logged users can fill out.
    2. Every time that the user fills the form there is a custom post field called invoice_amount that you want to update.
    3. That custom post type needs to have the format OLAB-XXXX, where the XXXX represents the incremental number that needs to be updated every time the user fills the form.

    So far it seems this might be doable with our Second Line Support team’s help, still, once you confirm the steps above we will check with them and provide more information about your request.

    Looking forward to hearing from you soon,
    Laura

  • Laura Zeballos
    • Staff

    Hi Fatima Buzdar ,

    Thanks for confirming and clarifying the information, we really appreciate it.

    I escalated your request to our Second Line Support, and they will confirm if this might be possible or not. Since they do work on very complex issues it could take more than usual to get a reply from them, we will keep you posted in this ticket about it.

    Your patience on this is widely appreciated it.

    Best regards,
    Laura

  • Prashant
    • Staff

    Hi Fatima Buzdar

    I hope you are doing well.

    We have worked on your request and found a solution. Please add a must-use plugin to your site’s wp-content/mu-plugins folder like this https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins, then add the following code to the plugin’s php file:

    <?php
    add_action ( 'forminator_form_after_handle_submit', 'wpmudev_update_profile_invoice_amount', 10, 2 );
    add_action( 'forminator_form_after_save_entry', 'wpmudev_update_profile_invoice_amount', 10, 2 );
    function wpmudev_update_profile_invoice_amount( $form_id, $response ) {
        if ( $form_id != 7446 ) { //Please change the form ID
            return;
        }
    
        if ( $response && is_array( $response ) ) {
    	if ( $response['success'] ) {
                $user_id = get_current_user_id();
                $invoice_amount = get_user_meta( $user_id, 'invoice_amount', true );
                $initial_string = 'OLAB-0001';
                if ( empty( $invoice_amount ) ) {
                    update_user_meta( $user_id, 'invoice_amount', $initial_string );
                } else {
                    $invoice_amount = str_replace('OLAB-', '', $invoice_amount);
                    $invoice_amount = $invoice_amount + 1;
                    $invoice_amount = sprintf("%04s", $invoice_amount);
                    update_user_meta( $user_id, 'invoice_amount', 'OLAB-'.$invoice_amount );
                }
            }
        }
    }

    Note: In the code please change 7446 to your form’s ID.

    We recommend to test this on the dev/staging version first before putting it on the live site.

    Kind Regards
    Prashant