WPForms自定义重定向不起作用

时间:2021-02-13 作者:elimariaaa

我在一个产品类别中使用WPForms,客户在填写表单后会被重定向到购物车。您在其中设置URL的WPForms重定向非常有效,但是,我需要URL是动态的。

我找到了一个带有所需脚本的git,并将其应用于我的函数。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 );
我还确保我的孩子主题有效,我的功能正常。正在使用php。

可能是什么问题?非常感谢您的帮助。

谢谢-Eli公司

1 个回复
SO网友:elimariaaa

我不知道为什么wpforms_process_redirect_url 不起作用。我用过this 相反

以下是我的完整代码:

//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();
    }
}

相关推荐

重置插件版本缓存|PRE_SET_SITE_TRANSPENT_UPDATE_PLUGINS

在我的插件中,我使用这些过滤器进行自动更新并检查许可证。add_filter( \'pre_set_site_transient_update_plugins\', array( &$this, \'check_for_update\' ) ); add_filter( \'plugins_api\', array( &$this, \'plugin_api_call\' ), 10, 3 ); 但正如我所看到的,WordPress在我的插件中更改了版本标签后,会调用该过