Reputation: 61
I am trying to redirect logged-out users to the "my account" page when they try to checkout.
I have tried this but it's not working as expected:
function wpse_131562_redirect() {
if (
! is_user_logged_in()
&& (is_cart() || is_checkout())
) {
wp_redirect('woocommerce_myaccount_page_id'());
exit;
}
}
add_action('template_redirect', 'wpse_131562_redirect');
Upvotes: 1
Views: 168
Reputation: 253784
Your code can't work as there is no redirect link… Try the following instead (redirecting unlogged user from Checkout to My Account):
add_action('template_redirect', 'unlogged_my_account_redirect');
function unlogged_my_account_redirect() {
if ( ! is_user_logged_in() && is_checkout() && ! is_wc_endpoint_url() ) {
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit();
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Upvotes: 1