• Hello,
    I’m building an integration with an external system that is almost working. I have the external event sending a POST request to a page where I execute Forminator_API::update_form_entry to set the new value for a hidden field.
    But here is my problem: I wanted to re-trigger email notifications based on the updated field of the entry but can’t make it work. I’ve found your code at https://gist.github.com/wpmudev-nebu/8156116010f009ab3a6823f74a24ecbe, but either the notification is not sent or the notification fields (all fields with {name}, not just the updated one) are all stripped.
    Can you tell me if my idea is achievable and point me in the right direction?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @danidada

    I hope you are doing well.

    You will need to use the Forminator_CForm_Front_Mail() class and then the process_mail() function.

    But note that custom coding is outside our support scope, for that, you’ll need to hire a developer to provide the required custom code for you. WordPress provides a jobs directory here https://jobs.wordpress.net/, if you need further advice about it, feel free to email wpsupport@incsub.com.

    Subject: ATTN: WPMU DEV support – wp.org

    Best Regards
    Patrick Freitas

    Thread Starter danidada

    (@danidada)

    Hello Patrick,

    I am using the class and function you mentioned but the data sent in the notification is the “old” data, not the updated entry data. I have used the same code as in the plugin.

                        // this one is the code updating the field value 
    Forminator_API::update_form_entry( $form_id, $entry_id, array( array('name'=>'hidden-3', 'value'=>"si") ) );

                        $error = '';

                        if ( isset( $entry_id ) ) {

                            $forminator_mail_sender = new Forminator_CForm_Front_Mail();

                            $entry                  = new Forminator_Form_Entry_Model( $entry_id );


                            if ( empty( $entry->form_id ) || ! empty( $entry->draft_id ) ) {

                                $error = esc_html__( 'Entry ID was not found.', 'forminator' );

                            }

                            $module_id = $entry->form_id;

                            Forminator_Front_Action::$module_id     = $module_id;

                            Forminator_Front_Action::$module_object = Forminator_Base_Form_Model::get_model( $module_id );

                            // Emulate Forminator_Front_Action::$prepared_data.

                            Forminator_Front_Action::$prepared_data = recreate_prepared_data( Forminator_Front_Action::$module_object, $entry );

                            // Emulate Forminator_Front_Action::$hidden_fields.

                            if ( ! Forminator_Front_Action::$module_object ) {

                                $error = esc_html__( 'Error: Module object is corrupted!', 'forminator' ) ;

                            }

                            Forminator_Front_Action::$module_settings = method_exists( Forminator_Front_Action::$module_object, 'get_form_settings' )

                            ? Forminator_Front_Action::$module_object->get_form_settings() : Forminator_Front_Action::$module_object->settings;

                            Forminator_CForm_Front_Action::check_fields_visibility();

                            $module_object = Forminator_Base_Form_Model::get_model( $module_id );
    // printed data is the updated one
                            var_dump($entry);
    //data sent by mail is the old one
                            $forminator_mail_sender->process_mail( $module_object, $entry );
    Thread Starter danidada

    (@danidada)

    Just to get you an update on the code I’m testing. Now it reflects new value on email notification content BUT it doesn’t fire if there is a condition based on the updated field value.

    //the update
    Forminator_API::update_form_entry( $form_id, $entry_id, array( array('name'=>'hidden-3', 'value'=>"si") ) );

                        $error = '';

                        if ( isset( $entry_id ) ) {

                            $get_data_from_entry = $this->data_from_enty($entry);

                            $submitted_data = Forminator_Core::sanitize_array( $get_data_from_entry );

                            // Send email.

                            $custom_form = Forminator_Form_Model::model()->load( $form_id );

                            if ( ! is_object( $custom_form ) ) {

                                $error .= json_encode(

                                    array(

                                        'message' => apply_filters(

                                            'forminator_draft_invalid_form_id',

                                            esc_html__( 'Invalid form ID.', 'forminator' ),

                                            $form_id

                                        ),

                                    )

                                );

                            }

                            $forminator_mail_sender       = new Forminator_CForm_Front_Mail();

                            $draft_entry                  = new Forminator_Form_Entry_Model( $entry_id );

                            // just to check var_dump($draft_entry);

                            Forminator_Front_Action::$prepared_data = array_replace( Forminator_Front_Action::$prepared_data, $submitted_data );

                            $mail_sent                    = $forminator_mail_sender->process_mail( $custom_form, $draft_entry );


                        }

    Can you please tell me what I am missing? Just ANY condition stop the notification from being sent, also for fields that wasn’t updated.

    • This reply was modified 1 week, 2 days ago by danidada.
    • This reply was modified 1 week, 2 days ago by danidada.
    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @danidada

    I asked our developers to see if you need to use a different hook or method to make this work.

    We will get back to you when having more updates.

    Best Regards,
    Amin

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @danidada

    We have further feedback from our developers.

    Forminator_API::update_form_entry() only updates the entry meta in the database. It does not automatically re-trigger notifications or rebuild the submission context that conditional notifications use.

    After calling the update_form_entry() function, it is important to follow the same pattern as Forminator’s resend-notification process. If only the modified field is injected using array_replace(), the notification conditions will not have the complete context of the submission. As a result, these conditions may evaluate to false, and the email notification could be skipped.

    I’m afraid since custom coding is out of scope of our support we can’t you further you need to hire a developer or review the plugin codes, as my colleague mentioned here.

    Best Regards
    Amin

    Thread Starter danidada

    (@danidada)

    Hello, thanks for your reply Amin and sorry for my delay.
    I understand your mention about the custom coding being out of scope, but let me just correct one thing in your reply: you wrote “If only the modified field is injected using array_replace(), the notification conditions will not have the complete context of the submission” but in here

    Forminator_Front_Action::$prepared_data = array_replace( Forminator_Front_Action::$prepared_data, $submitted_data );

    $submitted_data is extracted from the entry

    private function data_from_enty( $entry ) {
    $post_data = array();
    foreach ( $entry->meta_data as $key => $meta ) {

    if ( ! isset( $meta['value'] ) ) {
    continue;
    }
    $post_data[ $key ] = $meta['value'];
    }
    return $post_data;
    }

    so it contains all data, not only the edited one.

    The only way I’ve found to workaround the problem is to manually remove the conditions in the notifications ( in $custom_form->notifications before passing it as the first parameter of $forminator_mail_sender->process_mail ) but I don’t really like it (although it works) so if I need to pass the context as you said with another call like you wrote, could you enlighten me? Because I didn’t find any of it in the code.

    Thanks again

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hello @danidada

    I’m afraid that at this moment, there is currently no single public API method that both updates the entry and re-runs conditional notifications automatically. The only way to do it is through custom code, similar to what are you doing it.

    Kind regards,
    Jair.

Viewing 7 replies - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.