Site icon GetCodify

Plugin: Forcing the Login Page Before Checkout in Woocommerce

In woocommerce settings by default, there is an option to tell the user to login before checkout, but here in this article, we are trying to redirect to the login/register page if the user has not signed in when a user tries to enter the checkout page. 

Solution 1:

a. The best solution is to use a plugin that we have only created.

b. Download the plugin called “WooCommerce login before checkout” click on the download button below.

c. After downloading, upload in  WordPress “add new” plugin, and active. now you can check the checkout process and validate. (before check please check once you are currently logged out of site.)

Solution 2: 

  1. First, we need to (this step is optional because we can use the my-account default page for this) create a login/register page that you want to link.
  2. find the login/register page and its page id. How to find it? goto WordPress dashboard pages->  and find the my-account page and just hover on edit below the name you will find the page id as shown in below image.
  3. After getting the Page Id copy the below code to the functions.php file and paste if you are feeling difficulty in finding functions.php and paste you can use this plugin called: My custom functions and activate and paste it inside the textbox, save and then turn it on. and then just replace the $redirect_page_id=’your page id’ in the below code. 
function check_redirect_pre_checkout() {
	if ( ! function_exists( 'wc' ) ) return;

	$redirect_page_id = 10;
	if ( ! is_user_logged_in() && is_checkout() ) {
		wp_safe_redirect( get_permalink( $redirect_page_id ) );
		die;
	} elseif ( is_user_logged_in() && is_page( $redirect_page_id ) ) {
		wp_safe_redirect( get_permalink( wc_get_page_id( 'checkout' ) ) );
		die;
	}
}
add_action( 'template_redirect', 'check_redirect_pre_checkout' );
Exit mobile version