提交后,注册表单域重置(wordpress& woocommerce) [英] Registration form fields reset after submit (wordpress & woocommerce)

查看:159
本文介绍了提交后,注册表单域重置(wordpress& woocommerce)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个部分的woocommerce登记表:

- 一个给私人,

- 另一个给公司。



在公司选项中有两个附加字段。我可以通过单选按钮在私人和公司之间进行切换,然后查看相关字段。

问题:填写表单(作为私人用户)并犯一些错误时,表单重新加载并显示错误的位置(没关系)。

但不幸的是,在重新加载之后,它会加载所有字段的表单(包含公司字段的表单)。所以我需要在私人和公司之间点击2次以恢复正确的行为。



我该如何解决这个问题?我的意思是在重新加载这个错误之后,以最初的形式显示表单。



我不确定这是否是代码负责,但我们试试:

  add_filter('woocommerce_registration_errors','rs_registration_form_validation',10,3); 
函数rs_registration_form_validation($ reg_errors,$ sanitized_user_login,$ user_email)
{
global $ woocommerce;

$ company_fields_required =(!empty($ _ POST ['registration_type'])&'company'=== $ _POST ['registration_type']);
$ shipp_to_different_address =(!empty($ _ POST ['register_ship_to_different_address'])&& 1 == $ _POST ['register_ship_to_different_address']);

$ errors = false;
$ fields = rs_registration_form_fields();
if($ shipp_to_different_address){
$ fields + = rs_registration_form_fields_address();
}

if(!$ company_fields_required){
unset($ fields ['billing_company']);
unset($ fields ['billing_nip']);
}

//验证所需
foreach($ fields as $ field => $ settings){
if(false === isset($ settings [ 'required'])|| true === $ settings ['required']){
if(empty($ _ POST [$ field])){
$ errors = true;
wc_add_notice('Pole:< strong>'。$ settings ['label']。'< / strong> jest wymagane。','error');



$ b if($ errors){
return new WP_Error('registration-error','Proszępoprawićbłędyw formularzu' );
}

返回$ reg_errors;
}

add_action('woocommerce_created_customer','rs_registration_form_submit');
函数rs_registration_form_submit($ user_id)
{
$ fields = rs_registration_form_fields();
$ fields + = rs_registration_form_fields_address();
$ b $ foreach($ fields as $ field => $ settings){
if(isset($ _ POST [$ field])&&!empty($ _ POST [$ field] )){
update_user_meta($ user_id,$ field,$ _POST [$ field]);
}
}
}

add_filter('register_form','rs_registration_form');
函数rs_registration_form()
{
$ fields = rs_registration_form_fields();

包括'_rs_registration_form.php';
}

add_filter('register_form_address','rs_registration_form_address');
函数rs_registration_form_address()
{
$ fields = rs_registration_form_fields_address();

包括'_rs_registration_form.php';
}

add_filter('woocommerce_edit_address_slugs','rs_fix_address_slugs');
函数rs_fix_address_slugs($ slugs)
{
$ slugs ['billing'] ='billing';
$ slugs ['shipping'] ='运送';

返回$ slugs;
}

函数rs_rejestracja_url()
{
return get_permalink(244);
}

函数rs_logowanie_url()
{
return get_permalink(20);


函数rs_show_checkout_progress_bar($ step ='')
{
include'_checkout_progress_bar.php';
}

函数rs_order_form_buttons()
{
include'_order_form_buttons.php';
}

add_filter('woocommerce_get_checkout_url','rs_get_checout_url');
函数rs_get_checout_url($ url){
if(is_user_logged_in()){
$ url。='#step1';
}

return $ url;
}

包含'src / RS_Search.php';


解决方案

我不知道WooCommerce,但我认为错误结果,因为这些行:

  $ company_fields_required =(!empty($ _ POST ['registration_type'])&& 'company'=== $ _POST ['registration_type']); 

  if(!$ company_fields_required){
unset($ fields ['billing_company']);
unset($ fields ['billing_nip']);
}

提交您的私人表单并且验证失败后,您的字段为再次装载。现在可以,在你的$ _POST变量中,registration_type设置为'company'?如果您只是在函数的开头print_r您的$ _POST ['registration_type'],您可以测试它。如果情况并非如此,那么我很确定这个错误发生在另一个函数中,因为这对我来说是合理的。



编辑:再看一遍在你的代码中,我认为没有任何发布的函数会对这种不正当行为负责。第一个函数只负责检查是否有一些已发布的值缺失,并说嘿,这是一个错误。必须有另一个函数负责稍后显示在HTML中的字段。我认为你需要找到这个功能。


I've got woocommerce registration form with two sections:
- One for private person,
- the other for company.

In company option there is two additional fields. I can switch between private and company by radio buttons and then I see relevant fields.

Problem: When I fill the form (as private user) and make some mistake, form reload and show where is the error (that is ok).

But unfortunately, after reload, it loads the form with all fields (the ones with additional company fields too). So I need to click 2 times between private and company to restore the right behavior.

How can i fix this? I mean after this error reloading, to display the form as initially.

I don't be sure that this is code responsible for this, but let's try:

add_filter('woocommerce_registration_errors', 'rs_registration_form_validation', 10, 3);
function rs_registration_form_validation($reg_errors, $sanitized_user_login, $user_email)
{
    global $woocommerce;

    $company_fields_required = (!empty($_POST['registration_type']) && 'company' === $_POST['registration_type']);
    $shipp_to_different_address = (!empty($_POST['register_ship_to_different_address']) && 1 == $_POST['register_ship_to_different_address']);

    $errors = false;
    $fields = rs_registration_form_fields();
    if ($shipp_to_different_address) {
        $fields += rs_registration_form_fields_address();
    }

    if (!$company_fields_required) {
        unset($fields['billing_company']);
        unset($fields['billing_nip']);
    }

    //Validate required
    foreach ($fields as $field => $settings) {
        if (false === isset($settings['required']) || true === $settings['required']) {
            if (empty($_POST[$field])) {
                $errors = true;
                wc_add_notice('Pole: <strong>'.$settings['label'].'</strong> jest wymagane.', 'error');
            }
        }
    }

    if ($errors) {
        return new WP_Error('registration-error', 'Proszę poprawić błędy w formularzu');
    }

    return $reg_errors;
}

add_action('woocommerce_created_customer', 'rs_registration_form_submit');
function rs_registration_form_submit($user_id)
{
    $fields = rs_registration_form_fields();
    $fields += rs_registration_form_fields_address();

    foreach ($fields as $field => $settings) {
        if (isset($_POST[$field]) && !empty($_POST[$field])) {
            update_user_meta($user_id, $field, $_POST[$field]);
        }
    }
}

add_filter('register_form', 'rs_registration_form');
function rs_registration_form()
{
    $fields = rs_registration_form_fields();

    include '_rs_registration_form.php';
}

add_filter('register_form_address', 'rs_registration_form_address');
function rs_registration_form_address()
{
    $fields = rs_registration_form_fields_address();

    include '_rs_registration_form.php';
}

add_filter('woocommerce_edit_address_slugs', 'rs_fix_address_slugs');
function rs_fix_address_slugs($slugs)
{
    $slugs['billing'] = 'billing';
    $slugs['shipping'] = 'shipping';

    return $slugs;
}

function rs_rejestracja_url()
{
    return get_permalink(244);
}

function rs_logowanie_url()
{
    return get_permalink(20);
}

function rs_show_checkout_progress_bar($step = '')
{
    include '_checkout_progress_bar.php';
}

function rs_order_form_buttons()
{
    include '_order_form_buttons.php';
}

add_filter('woocommerce_get_checkout_url', 'rs_get_checout_url');
function rs_get_checout_url($url) {
    if (is_user_logged_in()) {
        $url .= '#step1';
    }

    return $url;
}

include 'src/RS_Search.php';

解决方案

I don't know WooCommerce, but I think the error results because of these lines:

$company_fields_required = (!empty($_POST['registration_type']) && 'company' === $_POST['registration_type']);

and

if (!$company_fields_required) {
    unset($fields['billing_company']);
    unset($fields['billing_nip']);
}

After you submitted your "private" form and the validation failed, your fields are loaded again. Could it now be, that in your $_POST variable the registration_type is somehow set to 'company'? You can test this if you just print_r your $_POST['registration_type'] at the beginning of the function. If that is not the case, then I'm pretty sure the bug happens in another function, because this makes sense to me so far.

EDIT: After taking another look on your code, I think none of the posted functions is responsible for the misbehaviour. The first function is only responsible to check if some of the posted values are missing and to say "hey, here is an error". There has to be another function which is responsible for the fields which later are displayed in your HTML. I think you need to find this function.

这篇关于提交后,注册表单域重置(wordpress&amp; woocommerce)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆