How to Disable Woocommerce My Account Autodirect - woocommerce

I'd like to customize the Woocommerce login/register page using an editor plugin which requires you to be logged into the Wordpress Dashboard. The problem is Woocommerce directs to the My Account page if the user is logged in. Is there a snippet I could temporarily run that will tell Woocommerce to load the login/register page instead of the My Account page if the user is logged in?

I found a solution thanks to a reply Oliver Vogel made to someone else asking the same question here. Thanks Oliver! This is Oliver's Post
For non-technical people like me who are trying to edit the Woocommerce Login / Register page while being logged into the Wordpress dashboard, insert this snippet into the functions.php file preferably with a nice plugin like Code Snippets. It will create a shortcode called [woo-login]. Create a new web page and add the [woo-login] shortcode to it and the Woo login page will open even when you are logged in.
add_shortcode('woo-login', 'my_login');
function my_login() {
if (is_user_logged_in()) {
ob_start();
echo '<div class="woocommerce">';
wc_get_template('myaccount/form-login.php');
echo '</div>';
return ob_get_clean();
} else {
ob_start();
echo '<div class="woocommerce">';
wc_get_template('myaccount/form-login.php');
echo '</div>';
return ob_get_clean();
}
}

Related

WooCommerce check and redirect to login before checkout

The default Woocommerce checkout behavior breaks a lot of web conventions by showing the "Create Account" boxes on the checkout page if it detects the user is not logged in. New users may not know what to do without any added instructions.
My desired sequence would be:
User Checkout > Check login >
Proceed to checkout if logged in.
Redirect to login/register page if not logged in > proceed to the checkout page.
This is EXACTLY the case in WooCommerce login redirect based on cart
However, what I feel uncomfortable is that, in the above case, once the user logged in, he/she will be redirected to the checkout page if the cart is not empty. If the user's cart is not empty, he/she will not be able to go to MyAccount at all even though he/she does not want to checkout yet.
Any idea on this one?
Try this no need to change core code
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
$pageid = get_option( 'woocommerce_checkout_page_id' );
if(!is_user_logged_in() && is_page($pageid))
{
$url = add_query_arg(
'redirect_to',
get_permalink($pagid),
site_url('/my-account/') // your my account url
);
wp_redirect($url);
exit;
}
if(is_user_logged_in())
{
if(is_page(get_option( 'woocommerce_myaccount_page_id' )))
{
$redirect = $_GET['redirect_to'];
if (isset($redirect)) {
echo '<script>window.location.href = "'.$redirect.'";</script>';
}
}
}
}
In the second answer there is a problem, after loging in the user always redirects checkout page, it does not stand on my-account page until the cart become empty!!
This code redirects the user to my-account page for login instead of checkout if the user is not logged in, then after loging in it automatically redirects to checkout page.
STEP 1:
Add this code in function.php
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
$pageid = get_option( 'woocommerce_checkout_page_id' );// your checkout page id
if(!is_user_logged_in() && is_page($pageid))
{
$url = add_query_arg(
'redirect_to',
get_permalink($pageid),
site_url('/my-account/') // your my acount url
);
wp_redirect($url);
exit;
}
}
STEP 2:
Add this code at the end of /wp-content/plugins/woocommerce/templates/myaccount/my-account.php
<?php
$quark_web_solution_redirect = $_GET['redirect_to'];
if (isset($quark_web_solution_redirect)) {
echo '<script>
window.location.href = "'.$quark_web_solution_redirect.'";
</script>';
}
?>
after reading this post and others in Stackoverflow, i came up with a simple solution (more than i thought at first) that is working for me... Maybe theres something i have missed up in the process, but i have tested several combinations and it works for me. So we want to:
Customer goes to checkout
If Logged In, continue to checkout. If not logged in, go to a page with login and register forms.
Once in this page, if customer logs in or register, continue to Checkout.
First, i created a custom page with [woocommerce_my_account] shortcode. Here i customized the texts etc... in the forms, so its a little bit different that my account page. URL path of this page is -> /iniciar-sesion-registrarse.
Then, i put this code in functions.php of my child theme:
function custom_woocommerce_login_redirect_to_checkout_page() {
// Caso 1: Usuario no identificado intenta acceder a Finalizar Compra
if ( !is_user_logged_in() && is_checkout() )
wp_redirect( get_permalink( get_page_by_path('iniciar-sesion-registrarse') ) );
// Caso 2: Si en la página de identificarse, el cliente ha accedido o se ha registrado, o tiene el carrito vacío, continuar a Finalizar Compra.
if ( is_page('iniciar-sesion-registrarse') ) {
if( is_user_logged_in() || WC()->cart->is_empty() ) {
wp_redirect( get_permalink( get_page_by_path('finalizar-comprar') ) );
}
}
}
add_action( 'template_redirect', 'custom_woocommerce_login_redirect_to_checkout_page' );
So in Case 1: If user is not logged in and tries to go to checkout, it redirects to my custom login page 'iniciar-sesion-registrarse'.
Then in Case 2: If user loggin or register in this page, normal my account page would be displayed in this page, then i redirect users who are logged in and are in this page to checkout. I also redirect them to checkout if someones access this custom page directly and has nothing in their carts (so woocommerce message: u cant checkout without items in ur cart is displayed).
This way, while loggin in or registering in this custom page, error messages will display normally as if it was real 'my-account' page, but once logged in, it will redirect to checkout.
I hope someone can tell me if i have missed something in the process. Thx
Actually had this problem today & now have a solution.
Context: our site has a separate user registration system than WooCommerce, so we didn't want a duplicative process prompting users to signup twice.
First I'll give the brief solution then list the full steps for my situation.
Brief solution: enable guest checkout, (trust me) enable account creation during checkout, automatically generate username and password (most important). Then disable in email settings the "New Account" message. Now "users" can checkout with account details auto-generated, but they never know about it and that information is essentially dumped.
Screenshot of my Accounts & Privacy settings. Please note I also disabled the "New Account" message triggered in the Email settings for WC.
Add this code in function.php
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
$pageid = get_option( 'woocommerce_checkout_page_id' );
if(!is_user_logged_in() && is_page($pageid))
{
$url = add_query_arg(
'redirect_to',
get_permalink($pagid),
site_url('/my-account/') // your my acount url
);
wp_redirect($url);
exit;
}
}

How to create separate log-in and registration pages in WooCommerce

I want to seperate the login and registration page for woocommerce.
(there is a different solution that I don't get to work here:
Separate registration page in WooCommerce website)
I duplicated the form-login.php and renamed it to register.php
In the register.php template I deleted the login-content.
I want to add a page to wordpress and add the register.php template with a SHORTCODE to that page. How do I do this??
Then I could add a Register Link to the Login Page with
<?php _e( ' Register' ); ?>
and the new shortcode register page is loaded.
mmmhhh...
what do I have to add to the duplicated login /now register.php, that it can be loaded to any page with a SHORTCODE? that would be interesting.
Thanks for helping out!Best wishes, Mika
A more straightforward way to do this is to save the contents of the original form-login.php as form-login-single.php, and then replace the file form-login.php with:
<?php
if( isset($_GET['action']) == 'register' ) {
wc_get_template( 'myaccount/form-register.php' );
} else {
wc_get_template( 'myaccount/form-login-single.php' );
}
This way you don't need to modify your functions.php further, plus you don't run into any trouble with double-rendering of the register form that I got when using #Hassan-ALi's method.
You can create a copy of the Woocommerce form-login.php and name it form-register.php. The form-login.php is located in /woocommerce/templates/myaccount/ folder.
Then in the form-login.php you can create a link to the form-register.php using this code
register
// Separete Login form and registration form
add_action('woocommerce_before_customer_login_form','load_registration_form', 2);
function load_registration_form(){
if(isset($_GET['action'])=='register'){
woocommerce_get_template( 'myaccount/form-registration.php' );
}
}

Wordpress & Gravity Forms - Why Is My For Only Display Form Admin & Not Post Author On FrontEnd?

I'm building a directory listings website where local businesses can add and edit their listings from the front-end of the website. I'm running into an issue where only administrators are able to view the form on the front end, but the listing author can't. Here is my conditional code:
<?php if(current_user_can( 'edit_others_posts', $post->ID ) || ($post->post_author == $current_user->ID)) { ?>
<?php
echo do_shortcode('[gravityform id="17" update=" '. $post->ID . '" title="false" ajax="true" description="false"]');
?>
<?php } ?>
I was able to find a fix yesterday. Apparently, to allow front-end edits to listings - you have to hook into a gravity forms settings and tell it to do so. Otherwise, non-admins won't be able to view the form.
// Allow gravity form editing on the front end
add_filter('gform_update_post/public_edit', '__return_true');

Woocommerce New Customer Admin Notification Email

I am building an ecommerce site using Wordpress and Woocommerce. I need the site to send out a notification email to the site administrator when a new customer account is registered. I thought this functionality would be built into Woocommerce since it uses the Wordpress user account structure and Wordpress sends new user notifications, but it doesn't appear to be. Does anyone know of a plugin or a function I can use to add this functionality? Thanks!
I'm assuming that you are using html inside emails. If you are using plain text the procedure is similar.
You need to override the woocommerce template structure. Here you can find how: http://docs.woothemes.com/document/template-structure/.
Actually the only file you need to override is your_template_directory/woocommerce/emails/customer-new-account.php.
At the end of this file add this line of code:
<?php do_action( 'new_customer_registered', $user_login ); ?>
In functions.php add this:
function new_customer_registered_send_email_admin($user_login) {
ob_start();
do_action('woocommerce_email_header', 'New customer registered');
$email_header = ob_get_clean();
ob_start();
do_action('woocommerce_email_footer');
$email_footer = ob_get_clean();
woocommerce_mail(
get_bloginfo('admin_email'),
get_bloginfo('name').' - New customer registered',
$email_header.'<p>The user '.esc_html( $user_login ).' is registered to the website</p>'.$email_footer
);
}
add_action('new_customer_registered', 'new_customer_registered_send_email_admin');
add_action('woocommerce_created_customer', 'admin_email_on_registration', 10 , 1);
function admin_email_on_registration( $customer_id) {
wp_new_user_notification( $customer_id );
}
woocommerce_created_customer is hook which is called when user created by woocommerce. It only sends notification to customer. we will use wp_new_user_notification() function to send notification to Admin.
I was pulling my hair out trying to figure this same issue out and after going back and forth with the developers the default is to not send new customer registration notification emails to admin.
After trying various email plugins and even resorting to using WP SMTP Email, I finally decided to leave it alone.
That said, WooCommerce 2.0 was released today so it may be built in the new version.
the answers is in the emails sections of "woocommerce / settings"
simply change the from email to wordpress#yourdomain.com
worked for me as i also had the same issues
To notify admin whn new user has registered use:
add_action( 'woocommerce_created_customer', 'woocommerce_created_customer_admin_notification' );
function woocommerce_created_customer_admin_notification( $customer_id )
{
wp_send_new_user_notifications( $customer_id, 'admin' );
}
See documentation on https://woocommerce.com/document/notify-admin-new-account-created/

Link to registration page in Wordpress

There is
wp_login_url, wp_logout_url, but what what about registration url?
Is there standard way to get link to registration? I need to display a link to registration page with further redirect to previous page.
PS I am using my theme login.
The following will return the registration url:
<?php
echo site_url('/wp-login.php?action=register');
?>
UPDATE:
To get the registration url with a redirect to the current page use:
<?php
echo site_url('/wp-login.php?action=register&redirect_to=' . get_permalink());
?>
Since 3.6, there is now a func:
http://codex.wordpress.org/Function_Reference/wp_registration_url
<?php echo wp_registration_url(); ?>
You can override it with the register_url filter.
add_filter( 'register_url', 'custom_register_url' );
function custom_register_url( $register_url )
{
$register_url = get_permalink( $register_page_id );
return $register_url;
}
I know this is an old question, but for anyone picking it up use wp_register().
It automatically determines if you're logged in and supplies either a link to the admin section of the site, or a link to the register form.
It also respects the settings in Settings -> General -> Membership (Anyone Can Register?)
If I understand correctly you are asking for default Word Press registration page. That would be www.domainname.com/wp-signup.php
<?php echo wp_registration_url(); ?>
https://codex.wordpress.org/Function_Reference/wp_registration_url
2 points here
Make sure you have turn on the "Anyone can register" in the setting page
if you host in a subfolder, make sure you include
Without subfolder:
Register
OR
Register
With subfolder
Register
In case of hosting your wordpress site in a subfolder (e.g.: mysite.com/myblog) you also need to include your site's url as follows:
<?php echo get_site_url() . "/wp-login.php?action=register" ?>
--> http://mysite.com/myblog/wp-login.php?action=register
Otherwise you will be redirected to a non-existing page
--> http://mysite.com/wp-login.php?action=register
1st of all I'm a WP newbe.
wp_registration_url can redirect the new user back to the page he came from, buy without enforcing login on the way (I dont use auto login because i want the user to authenticate the mail).
i use instead (sample with Allow PHP in posts plugin):
Register
hope it helps...
You can use wp_registration_url() any where you want to add link to wordpress registration
to add the redirection url use apply_filters()
<a href="<?php wp_registration_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ); ?>" >click to register</a>

Resources