如何解决自定义注册页面不输出任何错误消息
我通过从 WooCommerce (my_account) 复制模板,在自己的插件中创建了一个自定义注册页面。 一切正常,但是当客户注册时我无法输出错误消息,例如未填写字段时。
我用 billing_first_name 和 billing_last_name 尝试过,但我什么也没得到(只重新加载页面)。至少,没有创建客户,因此验证有效。在 WooCommerce 的 my_account-Page 上,输出错误消息。
add_shortcode( 'wc_reg_form_bbloomer','b2b_login_register' );
add_filter( 'woocommerce_registration_errors','bbloomer_validate_woo_account_registration_fields',10,3 );
function bbloomer_validate_woo_account_registration_fields( $errors,$username,$email ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error',__( '<strong>Error</strong>: First name is required!','woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error','<strong>Error</strong>: Last name is required!.' );
}
return $errors;
}
add_action( 'user_register','bbloomer_save_woo_account_registration_fields',1 );
function bbloomer_save_woo_account_registration_fields( $customer_id ) {
if ( isset( $_POST['company'] ) ) {
update_user_Meta( $customer_id,'billing_company',sanitize_text_field( $_POST['company'] ));
}
}
function b2b_login_register() {
if ( is_admin() ) return;
if ( is_user_logged_in() ) {
$my_account_url = wc_get_page_permalink( 'myaccount' );
header("Location:" . $my_account_url );
exit();
}
do_action( 'woocommerce_before_customer_login_form' ); ?>
<?PHP
// HERE I ADDED THE NOTICE - IT ONLY WORKS AT THIS PLACE AND IN THIS ORDER,OB_START() DOESN'T WORK BEFORE WC_PRINT_NOTICES();
wc_print_notices();
ob_start();
?>
<?PHP if ( 'yes' === get_option( 'woocommerce_enable_myaccount_registration' ) ) : ?>
<div class="u-columns col2-set" id="customer_login">
<div class="u-column1 col-1">
<?PHP endif; ?>
<h2><?PHP esc_html_e( 'Login','woocommerce' ); ?></h2>
<form class="woocommerce-form woocommerce-form-login login" method="post">
<?PHP do_action( 'woocommerce_login_form_start' ); ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="username"><?PHP esc_html_e( 'Username or email address','woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="username" autocomplete="username" value="<?PHP echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" /><?PHP // @codingStandardsIgnoreLine ?>
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="password"><?PHP esc_html_e( 'Password','woocommerce' ); ?> <span class="required">*</span></label>
<input class="woocommerce-Input woocommerce-Input--text input-text" type="password" name="password" id="password" autocomplete="current-password" />
</p>
<?PHP do_action( 'woocommerce_login_form' ); ?>
<p class="form-row">
<label class="woocommerce-form__label woocommerce-form__label-for-checkBox woocommerce-form-login__rememberme">
<input class="woocommerce-form__input woocommerce-form__input-checkBox" name="rememberme" type="checkBox" id="rememberme" value="forever" /> <span><?PHP esc_html_e( 'Remember me','woocommerce' ); ?></span>
</label>
<?PHP wp_nonce_field( 'woocommerce-login','woocommerce-login-nonce' ); ?>
<button type="submit" class="woocommerce-button button woocommerce-form-login__submit" name="login" value="<?PHP esc_attr_e( 'Log in','woocommerce' ); ?>"><?PHP esc_html_e( 'Log in','woocommerce' ); ?></button>
</p>
<p class="woocommerce-LostPassword lost_password">
<a href="<?PHP echo esc_url( wp_lostpassword_url() ); ?>"><?PHP esc_html_e( 'Lost your password?','woocommerce' ); ?></a>
</p>
<?PHP do_action( 'woocommerce_login_form_end' ); ?>
</form>
<?PHP if ( 'yes' === get_option( 'woocommerce_enable_myaccount_registration' ) ) : ?>
</div>
<div class="u-column2 col-2">
<h2><?PHP esc_html_e( 'Register','woocommerce' ); ?></h2>
<form method="post" class="woocommerce-form woocommerce-form-register register" <?PHP do_action( 'woocommerce_register_form_tag' ); ?> >
<?PHP do_action( 'woocommerce_register_form_start' ); ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide" id="company">
<label for="reg_company"><?PHP esc_html_e( 'Company','woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="company" id="reg_company" autocomplete="company" value="<?PHP echo ( ! empty( $_POST['company'] ) ) ? esc_attr( wp_unslash( $_POST['company'] ) ) : ''; ?>" /><?PHP // @codingStandardsIgnoreLine ?>
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_billing_first_name"><?PHP esc_html_e( 'First name','woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="billing_first_name" id="reg_billing_first_name" autocomplete="given_name" value="<?PHP echo ( ! empty( $_POST['billing_first_name'] ) ) ? esc_attr( wp_unslash( $_POST['billing_first_name'] ) ) : ''; ?>" /><?PHP // @codingStandardsIgnoreLine ?>
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_billing_last_name"><?PHP esc_html_e( 'Last name','woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="billing_last_name" id="reg_billing_last_name" autocomplete="family-name" value="<?PHP echo ( ! empty( $_POST['billing_last_name'] ) ) ? esc_attr( wp_unslash( $_POST['billing_last_name'] ) ) : ''; ?>" /><?PHP // @codingStandardsIgnoreLine ?>
</p>
<?PHP if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) ) : ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_username"><?PHP esc_html_e( 'Username','woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="reg_username" autocomplete="username" value="<?PHP echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" /><?PHP // @codingStandardsIgnoreLine ?>
</p>
<?PHP endif; ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_email"><?PHP esc_html_e( 'Email address','woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="email" id="reg_email" autocomplete="email" value="<?PHP echo ( ! empty( $_POST['email'] ) ) ? esc_attr( wp_unslash( $_POST['email'] ) ) : ''; ?>" /><?PHP // @codingStandardsIgnoreLine ?>
</p>
<?PHP if ( 'no' === get_option( 'woocommerce_registration_generate_password' ) ) : ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_password"><?PHP esc_html_e( 'Password','woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="woocommerce-Input woocommerce-Input--text input-text" name="password" id="reg_password" autocomplete="new-password" />
</p>
<?PHP else : ?>
<p><?PHP esc_html_e( 'A password will be sent to your email address.','woocommerce' ); ?></p>
<?PHP endif; ?>
<?PHP do_action( 'woocommerce_register_form' ); ?>
<p class="woocommerce-form-row form-row">
<?PHP wp_nonce_field( 'woocommerce-register','woocommerce-register-nonce' ); ?>
<button type="submit" class="woocommerce-Button woocommerce-button button woocommerce-form-register__submit" name="register" value="<?PHP esc_attr_e( 'Register','woocommerce' ); ?>"><?PHP esc_html_e( 'Register','woocommerce' ); ?></button>
</p>
<?PHP do_action( 'woocommerce_register_form_end' ); ?>
</form>
</div>
</div>
<?PHP endif; ?>
<?PHP do_action( 'woocommerce_after_customer_login_form' );
}
解决方法
尝试使用这个 df["geocode_result"] = gmaps.geocode(df["Location"])
钩子代替 woocommerce_process_registration_errors
。
我已经更新了代码。 函数的参数是4个而不是3个(如果都用了的话)。
https://woocommerce.github.io/code-reference/files/woocommerce-includes-class- wc-form-handler.html#source-view.1078
$validation_error = apply_filters( 'woocommerce_process_registration_errors',$validation_error,$username,$password,$email );
所以更新后的函数将是:
woocommerce_registration_errors
它对我有用。但我不使用简码,而是使用模板。在您的情况下,请尝试在此处查看:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。