2

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?

5 Answers 5

7

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>';
            }
    
        }
        }
    }
1
  • I am getting the message ERR_TOO_MANY_REDIRECTS. Does anybody faced the same issue?
    – Bill
    Dec 2, 2022 at 8:54
7

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>';
}
?>
2
  • 1
    Adding code in plugin files is a bad practice. You will lose all custom code in the next plugin update. You can use the action 'woocommerce_account_content' to add the code from step 2.
    – Joris
    Nov 4, 2021 at 15:05
  • You can also add the code in Step 2 by using a child theme, copying wp-content/plugins/woocommerce/templates/myaccount/my-account.php to wp-content/<your child theme>/woocommerce/myaccount/my-account.php adding it to the end of that.
    – Maytha8
    Jan 4, 2022 at 2:27
0

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:

  1. Customer goes to checkout
  2. If Logged In, continue to checkout. If not logged in, go to a page with login and register forms.
  3. 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

1
  • If u are using cache plugins, make sure to never cache this custom login page.
    – ahumado
    Apr 24, 2018 at 14:14
0

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.

enter image description here

1
  • 3
    A WARNING NOTE TO ALL READERS: when someone online tells you to "Trust me", please be on your guard and use your common sense before you act on that. I am keeping this answer now just for educational purpose to make all readers aware that there are people out in the open who always use your innocence to their advantage to do bad things. This is a case in point. Nov 9, 2018 at 1:29
0

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;
    }
}

2
  • The above redirects unlogged-in users to MyAccount after login, in the middle of a transaction. How about: When users click on "Checkout" (the "checkout" button will pass on a value to indicate users clicked on the "checkout" button) --> check log in or not, AND pass on another value if "checkout value" detected; --> redirect to form-login.php if users not logged in; --> form-login.php detects "checkout value; --> IF "checkout value" not null --> checkout page; ELSE means users come from other links with no "checkout value" -->MyAccount Page. I am not sure how this can be done. Jan 30, 2018 at 16:48
  • I happened to find another question similar to mine in: stackoverflow.com/questions/37200254/… . But I tried and it doesn't work. Anyone has any thoughts? Thank you in advanced. Jan 31, 2018 at 16:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.