WooCommerce check and redirect to login before checkout

て烟熏妆下的殇ゞ 提交于 2021-02-07 04:17:06

问题


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? Thanks in advanced everyone.


回答1:


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!!

Here is the Solution with DEMO.
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 = 68; // your 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;
    }
}

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

This post is from developer of Quark Web Solution.
Thank You!!




回答2:


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



回答3:


Add this code in function.php

add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
    $pageid = 68; // your 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;
    }
}



回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/48477504/woocommerce-check-and-redirect-to-login-before-checkout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!