我在一个产品类别中使用WPForms,其中客户在填写表单后被重定向到购物车。您所设置的WPForms重定向非常有效,但是,我需要该URL是动态的。
我用我需要的脚本找到了一个git,并将它应用到我的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 );我还确保我的子主题正常工作,我的functions.php也被使用了。
有什么问题吗?任何帮助都是非常感谢的。
谢谢!-Eli
发布于 2021-02-13 18:06:25
我不知道为什么wpforms_process_redirect_url不能工作。我用的是这。
这是我的完整代码:
//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();
}
}https://wordpress.stackexchange.com/questions/383328
复制相似问题