Woocommerce users are not authorized, redirection

I do an online store with Woocommerce, where my client asks me that he does not want people to be able to register, he will create a user and password for each of his customers. In this way, he can control who is buying at his store.

So, I went into Woocommerce and turned off registration at checkout and everywhere, and also allowed guests to place orders. Everything works fine, except that when someone tries to place an order, when he logs out, when he tries to go to the checkout page, he simply displays an unformatted message that says: "You must be logged in to place order". Is there a way in which I can redirect unregistered clients to the login page when trying to access the checkout?

+4
source share
3 answers

Perhaps this code may be more compact, simple and convenient:

add_action('template_redirect','check_if_logged_in');
function check_if_logged_in() {
    if(!is_user_logged_in() && is_checkout())
        wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
}

function.php ( ), .

.


: WooCommerce

+2

functions.php

add_action( 'template_redirect', 'redirect_user_to_login_page' );

function redirect_user_to_login_page(){
    // Make sure your checkout page slug is correct
    if( is_page('checkout') ) {
        if( !is_user_logged_in() ) {
            // Make sure your login page slug is correct in below line
            wp_redirect('/my-account/');
        }
    }
}
+1

! - , .

, functions.php :

add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{

    if(!is_user_logged_in() && is_checkout())
    {
        $url = add_query_arg(
            'redirect_to',
            get_permalink($pagid),
            site_url('/my-account/')
        );
        wp_redirect($url);
        exit;
    }
}
Hide result

0

Source: https://habr.com/ru/post/1655953/


All Articles