Ad

Add Link To "You Must Be Logged In To Checkout" Message In WooCommerce

In the WooCommerce setting page I set the customers to NOT be allow to place an order without an account. Then the customer must be logged to do the checkout. I have no problem with this setting. It works fine.

Now if the customer is not logged this message is shown "You must be logged in to checkout." I just need to add a link at the end of this message.

I use this code which allows to change the text and add a link

function filter_woocommerce_checkout_must_be_logged_in_message( $message ) {
    $message = 'You must be logged in to checkout. <a href="#" id="login-register">login / Register</a>';
    return $message; 
}
add_filter( 'woocommerce_checkout_must_be_logged_in_message', 'filter_woocommerce_checkout_must_be_logged_in_message', 10, 1 );

But the problem is that the html (the link) is printed as it (not rendered as a link).

Take a look at my capture to see what I mean.

enter image description here

Ad

Answer

That's because the filter is passed on to esc_html()

This line is copied from /templates/checkout/form-checkout.php line 26 @version 3.5.0

echo esc_html( apply_filters( 'woocommerce_checkout_must_be_logged_in_message', __( 'You must be logged in to checkout.', 'woocommerce' ) ) );

So if you really want to add HTML you will have to overwrite the template file. The template file can be overridden by copying it to yourtheme/woocommerce/checkout/form-checkout.php.

Replace

echo esc_html( apply_filters( 'woocommerce_checkout_must_be_logged_in_message', __( 'You must be logged in to checkout.', 'woocommerce' ) ) );

With

echo esc_html( apply_filters( 'woocommerce_checkout_must_be_logged_in_message', __( 'You must be logged in to checkout.', 'woocommerce' ) ) ) . '&nbsp;<a href="#" id="login-register">login / Register</a>';
Ad
source: stackoverflow.com
Ad