I'm trying to create a custom registration link on the checkout page. Here's the snippet I used:
add_action('woocommerce_checkout_fields', 'add_registration_custom_link');
function add_registration_custom_link(){
if(is_checkout()){
if (!is_user_logged_in()){
$info_message = apply_filters( 'woocommerce_checkout_login_message', __( 'New customer?', 'woocommerce' ) );
$info_message .= ‘<a href="http://www.mysite.co.uk?redirect_to=http://www.mysite.co.uk/checkout">’ . __( ‘Click here to Register’, ‘woocommerce’ ) . ‘</a>’;
wc_print_notice( $info_message, 'notice' );
}
}
}
The idea is to print a message + link that prompts users to register/log in if not already registered/logged in. It will replace the default login form at the top of the checkout page. I want to redirect users to the My Account page when they click on the "Click here to register" link so they can log in/register, and then redirect them back to checkout once successful.
So far, the code above isn't working and is generating additional issues on the page (i.e. invalid arguments in the shipping and billing forms). Any ideas how to get this to work properly?
UPDATE: I found this handy snippet and added it to my functions.php file:
function woocommerce_custom_redirects() {
// Case1: Non logged user on checkout page (cart empty or not empty)
if ( !is_user_logged_in() && is_checkout() )
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
}
add_action('template_redirect', 'woocommerce_custom_redirects');
This redirects the user to the My Account page if not logged in. I need an extra argument to redirect them back to the checkout page once logged in/registered. Any ideas?
Thanks!