javascript - 加载错误 wc_print_notices 以更正 woocommerce 登录或注册表单
问题描述
我已经在弹出元素中编写了 woocommerce 登录和注册表单。我还调用了<?php wc_print_notices(); ?>
弹出窗口,以便在适用时给出错误消息。我面临的挑战是在提交后显示正确的表单以及相关的登录或注册错误。
这是我在弹出窗口中使用的 php 表单。弹出窗口显示一个表单,具体取决于用户选择的是标题sign in
还是register
标题。这一切都有效。
function separate_registration_form() {
if ( is_admin() ) return;
ob_start();
if ( is_user_logged_in() ) {
wc_print_notices();
} else {
?>
<?php wc_print_notices(); ?>
<form id="woo-register-form" method="post">...etc...</form>
<form id="woo-login-form" method="post">...etc...</form>
<?php
}
return ob_get_clean();
}
我目前的解决方案是当类.woocommerce-error
在 DOM 中时添加一个 JS 触发器以显示弹出窗口。但在这种情况下,它显示了两种形式,我正在寻找如何调用与链接错误相关的正确形式。因此,当有人输入错误的登录密码时,页面加载后会显示带有登录表单的弹出窗口。这可以用 JS 或 PHP 完成吗?还是组合?
解决方案
在深入研究了表单的功能之后,我提出了以下解决方案。
表单需要有一个action
属性。所以这个action
属性将在提交表单后出现在地址栏中。然后您可以添加 JS 来定义action
名称是否在地址路径中以及是否.woocommerce-error
在 DOM 中以触发弹出窗口。
所以像这样:
function separate_registration_form() {
if ( is_admin() ) return;
ob_start();
if ( is_user_logged_in() ) {
wc_print_notices();
} else {
?>
<?php wc_print_notices(); ?>
<form id="woo-register-form" action="#register" method="post">...etc...</form>
<form id="woo-login-form" action="#login" method="post">...etc...</form>
<?php
}
return ob_get_clean();
}
让我们假设login-trigger
和register-trigger
是显示表单的按钮。重要的是在页面加载后添加这些操作。JS 将是:
$(window).on('load', function(){
/*check if #register is in url*/
if(location.href.indexOf("/#register") > -1){
/*check if error is in the DOM*/
if( $('.woocommerce-error').length){
/*all good, fire off the popup*/
$('.register-trigger').trigger('click');
}
}
/*same for login*/
if(location.href.indexOf("/#login") > -1){
if( $('.woocommerce-error').length){
$('.login-trigger').trigger('click');
}
}
});
推荐阅读
- c# - 从所选文件夹加载 X509Certificates
- android - 如何在 Android 工作室中将我的按钮向下移动?他们保持领先,固定
- c - 在 C 中使用 !(xa) 代替 x==a
- powershell - 使用 office 365 邮件 api 的 Powershell 脚本不再有效
- python - 使用“For”循环很难
- javascript - OrderBy 对象数组集合 Angular Js 出错
- ios - Ionic3 Linkedin 原生 SDK
- c++ - c ++:lambda表达式的输入
- android - 如何使用键盘 Enter 关闭 AlertDialog?
- javascript - 使用打字稿解压缩文件