One of my client want to disable woocommerce account creating form and place a custom link in checkout page if a user not logged in.
At that moment you’ll get the message “You must be logged in to checkout”.
To improve that customers experience doesn’t have to search where to click next, I added extra text with a hyperlink to the “my account/registration” page.
Find this code here
wp-content/plugins/woocommerce/templates/checkout/form-checkout.php
// If checkout registration is disabled and not logged in, the user cannot checkout.
if ( ! $checkout->is_registration_enabled() && $checkout->is_registration_required() && ! is_user_logged_in() ) {
echo esc_html( apply_filters( 'woocommerce_checkout_must_be_logged_in_message', __( 'You must be logged in to checkout.', 'woocommerce' ) ) );
return;
}
Replace the code with the this:
// If checkout registration is disabled and not logged in, the user cannot checkout.
if ( ! $checkout->is_registration_enabled() && $checkout->is_registration_required() && ! is_user_logged_in() ) {
$the_url = '/my-account';
echo esc_html( apply_filters( 'woocommerce_checkout_must_be_logged_in_message', __( 'You must be logged in to checkout.', 'woocommerce' ) ) );
printf(
esc_html__( 'Some text %1$s extra text, extra text', 'woocommerce' ),
sprintf(
'<a href="%s">%s</a>',
$the_url,
esc_html__( 'My account', 'woocommerce' )
)
);
return;
}
Note: You should keep a backup before do the above process. and i recommend you should do this to your child them. Follow the steps to do this
FAQ: How to add a extra link for logged out user in checkout form?
Answer: Follow the steps to add a extra link:
astra-child/woocommerce/checkout/form-checkout.php
wp-content/plugins/woocommerce/templates/checkout/form-checkout.php
// If checkout registration is disabled and not logged in, the user cannot checkout.
if ( ! $checkout->is_registration_enabled() && $checkout->is_registration_required() && ! is_user_logged_in() ) {
$the_url = ‘/my-account’;
echo esc_html( apply_filters( ‘woocommerce_checkout_must_be_logged_in_message’, ( ‘You must be logged in to checkout.’, ‘woocommerce’ ) ) ); printf( esc_html( ‘Some text %1$s extra text, extra text’, ‘woocommerce’ ),
sprintf(
‘%s‘,
$the_url,
esc_html__( ‘My account’, ‘woocommerce’ )
)
);
return;
}
Thanks for reading the post. if you having any issue shout in comment.
Rather than redirect buttons it would be good to have the register & login forms here which then after filled in direct back to checkout.
Just add this code in functions.php 🙂
function redirect_to_specific_page()
{
if ( is_page(‘checkout’)&& get_current_user_id() == null )
{
wp_redirect( ‘/login’, 301 );
exit;
}
}
note: /login is your custom slug of login/register
Thank you for your comment…