Title: Load error wc_print_notices to correct woocommerce login or registration form
Tags: javascript;php;woocommerce;pageload
Question: I have coded the woocommerce login and register forms in a popup element. I have also called the <?php wc_print_notices(); ?&gt; into the popup so error messages are given when applicable. The challenge I face is to show the correct form with the related log in or registration error after submission.
This the php form I use within the popup. The popup shows one form, depending if the user selected to sign in or register in the header. This all works.
function separate_registration_form() {
if ( is_admin() ) return;
ob_start();
if ( is_user_logged_in() ) {
wc_print_notices();
} else {
?&gt;
<?php wc_print_notices(); ?&gt;
<form id="woo-register-form" method="post"&gt;...etc...</form&gt;
<form id="woo-login-form" method="post"&gt;...etc...</form&gt;
<?php
return ob_get_clean();
My current solution is to add a JS trigger when the class .woocommerce-error is in the DOM in order to show the popup. But in this case it shows both forms, and I am looking how to call the right form related to the linked error. So when someone enters an incorrect login password, after page load it shows the popup with login form for example. Can this be done with JS or PHP? Or a combination?


Here is another answer: After some digging into the functionallity of the forms, I came with the following solution.
The Forms need to have an action attribute. So this action attribute will be in the address-bar after submitting the form. Then you could add JS to define if the action name is in the address path and if .woocommerce-error is in the DOM to trigger the popup.
So like this:
function separate_registration_form() {
if ( is_admin() ) return;
ob_start();
if ( is_user_logged_in() ) {
wc_print_notices();
} else {
?&gt;
<?php wc_print_notices(); ?&gt;
<form id="woo-register-form" action="#register" method="post"&gt;...etc...</form&gt;
<form id="woo-login-form" action="#login" method="post"&gt;...etc...</form&gt;
<?php
return ob_get_clean();
Let's assume that login-trigger and register-trigger are the buttons to show the forms. Important is to add these actions after page load. The JS will be:
$(window).on('load', function(){
/*check if #register is in url*/
if(location.href.indexOf("/#register") &gt; -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") &gt; -1){
if( $('.woocommerce-error').length){
$('.login-trigger').trigger('click');
}

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:08/01/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><php><woocommerce><pageload>', 'Title': 'Load error wc_print_notices to correct woocommerce login or registration form', 'ViewCount': 730, 'id': 57318046, 'user_AboutMe': '<p>Website developer, expert in Wordpress. Advanced developer in HTML, CSS and JS. Intermediate with MySQL. Beginner with PHP. Very keen to learn and hopefully one day an advanced user.</p>\n\n<p>Developed websites: shops (woocommerce based), charity websites, agency websites, social network website (peepso based). </p>\n\n<p>Send me an email if you would like to know more: contact@montensiscapital.com</p>\n', 'user_AccountId': 13335901.0, 'user_DisplayName': 'Demian', 'user_Reputation': 416.0, 'user_Views': 120.0, 'user_id': 9625779.0}}