I’m doing a shop online with Woocommerce, where my client ask me he don’t want people to be able to register, he will create a user and password for each one of his clients. That way he can control who buys on his shop.
So I went into Woocommerce and disable the registration at checkout and everywhere, and the option to allow guests to place orders. Everything works fine, except that when someone tries to place an order, when logged out, when he tries to go to the checkout page, it just shows an unformatted message saying “You must be logged in to place an order”. Is there a way where I can redirect not logged in customers to login page, when trying to access checkout?
Advertisement
Answer
May be this code could 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') ) ); }
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and fully functional.
Reference: WooCommerce login redirect based on cart