I have a WC site and have some additional user fields to populate when registering a user (meaning I cannot use the standard WC user form creation).
Now, I would like to offer the possibility for a visitor to create his own account in the product page.
This means he has chosen a product, and then, needs to create his account in order to be able to click on the "BUY" button.
In a first step, I was thinking to place the button into a conditional output (user logged in), but this is not working as the Cred user form in integrated inside a modal box (placed on the same page than the product page) and the user is not automatically loggedin after having created his account.
So for me, the best would be to have the following way of working :
- The visitor reaches the product page
- He has the ability either to login or to create his account thanks to a modal box
- If he has not account, he needs to populate the user form in the modal box
- After that, the account is created and he is logged in automatically and be able to click on the BUY button (which is displayed because he is logged).
It sounds like you already have your modal form set up and the problem is that the user is not automatically logged in when they use the form to register.
You can try the following code snippet to do that:
/**
* Auto-login new CRED user
*/
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
if ( 6 == $form_data['id'] ) { // Edit as required
if ( !empty( $_POST['user_login'] ) && !empty( $_POST['user_pass'] ) ) {
// get the user credentials from the $_POST object
$user = array(
'user_login' => $_POST['user_login'],
'user_password' => $_POST['user_pass'],
'remember' => true
);
$login = wp_signon( $user, false );
if ( is_wp_error($login) ) {
error_log( $login->get_error_message() );
}
}
}
}
You may find you need to reload the page after the form is submitted, though.
An alternative would be to stick with the WC workflow to register at the checkout, and customise its registration form to be able to add your custom fields to the registration page.
There are quite a few tutorials about doing that online, this one looks to be a good example: enlace oculto
I'm just a bit concerned with the need of page reloading.
Is there a way in the Form to reload the page after the form is submitted by pointing in the current page. I have not see such a feature in the list? (It is possible to define a specific page, but here, we need to manage this dynamically because there are several products in this site and the user creation form cold be on each of these pages.
Without trying myself I'm not sure if that would be entirely effective when using a user form submitted via ajax in a modal, you will have to do some testing to see.
An alternative would be to display a message, and have your message be a false link that simply reloads the current page, something like: