简体   繁体   English

提交后重新设置注册表单字段(wordpress和woocommerce)

[英]Registration form fields reset after submit (wordpress & woocommerce)

I've got woocommerce registration form with two sections: 我有两个部分的woocommerce注册表格:
- 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: 我不知道WooCommerce,但是我认为由于以下几行而导致错误:

$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'? 现在可能是在您的$ _POST变量中,registration_type以某种方式设置为“ company”吗? You can test this if you just print_r your $_POST['registration_type'] at the beginning of the function. 如果仅在函数开始时打印$ _POST ['registration_type'],则可以进行测试。 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. 必须有另一个功能负责随后在HTML中显示的字段。 I think you need to find this function. 我认为您需要找到此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM