0

I use WPForms in one product category where customers get redirected to cart after filling in the form. The WPForms redirection where you set the URL works perfectly, however, I need the URL to be dynamic.

I found a git with the script that I need and applied it to my functions.php:

function wpf_custom_redirect( $url, $form_id, $fields ) {

    // Only consider changing the redirect if its for form #50
    $variation_id = 0;
    $url = get_site_url();
    if ( '797' == $form_id ) {

        // In the example below, we look at the submitted value for field #5
        // If that value is "test", then we change the redirect URL
        if ( !empty( $fields['4']['value'] ) && 2 == $fields['4']['value'] ) { //birthdays
            $variation_id = 806;
        } else if ( !empty( $fields['4']['value'] ) && 1 == $fields['4']['value'] ) { //encouragement
            $variation_id = 807;
        } else if ( !empty( $fields['4']['value'] ) && 4 == $fields['4']['value'] ) { //spiritual
            $variation_id = 809;
        } else if ( !empty( $fields['4']['value'] ) && 5 == $fields['4']['value'] ) { //greeting
            $variation_id = 810;
        } else if ( !empty( $fields['4']['value'] ) && 6 == $fields['4']['value'] ) { //holiday
            $variation_id = 808;
        }

        $url .= '/cart?add-to-cart=82&variation_id='.$variation_id;
    }

    return $url;
}
add_filter( 'wpforms_process_redirect_url', 'wpf_custom_redirect', 10, 3 );

I also made sure that my child theme is working and my functions.php is being used.

What could be the issue? Any help is highly appreciated.

Thanks! -Eli

0

I'm not sure why wpforms_process_redirect_url doesn't work. I used this instead.

Here's my full code:

//Capture the wpform submit, and call the "processForm" function
add_action( 'wpforms_process_complete', 'processForm', 5, 4 );

function processForm( $form_fields, $entry, $form_data, $entry_id ) {

    $card_arr = array(797, 1104, 1103, 1102, 1101, 1038, 997, 996, 973, 812);
    $form_id = $form_data['id'];
    $variation_id = 0;
    $url = get_site_url();

    // Get the full entry object
    $entry = wpforms()->entry->get( $entry_id );

    // Fields are in JSON, so we decode to an array
    $entry_fields = json_decode( $entry->fields, true );

    //$form_data contains the user inputs
    //here you could validate your form
    if(!in_array($form_id, $card_arr)) {
        exit();
    } else if ( in_array($form_id, $card_arr)) {
        if($form_id == 797){
            if ( $entry_fields[4]['value_raw'] == 2 ) { //birthdays
                $variation_id = 806;
            }
            if ( $entry_fields[4]['value_raw'] == 1 ) { //encouragement
                $variation_id = 807;
            }
            if ( $entry_fields[4]['value_raw'] == 4 ) { //spiritual
                $variation_id = 809;
            }
            if ( $entry_fields[4]['value_raw'] == 5 ) { //greeting
                $variation_id = 810;
            }
            if ( $entry_fields[4]['value_raw'] == 6 ) { //holiday
                $variation_id = 808;
            }
        }
        $url .= '/cart/?add-to-cart=82&variation_id='.$variation_id;
        wp_redirect($url);
        exit();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.