WooCommerce Registration Shortcode – Error messages problems

I am currently creating a widget to display the registration form on a WordPress website that uses WooCommerce. For now, I only have 3 basic fields which are email, password, repeat password. I’m looking forward to add more WooCommerce fields, but want to solve that problem before jumping to the next step.

I’m having some problems with the messages output (wrong password, account already exists, etc).

Read More

I searched on the web and there was no shortcode already built for WooCommerce registration, beside their registration page. So I went ahead and created a shortcode, with a template part.

function custom_register_shortcode( $atts, $content ){
    global $woocommerce;
    $form = load_template_part('framework/views/register-form');
    return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );

This is a snippet I use to get the template part inside a variable, since the default function would “echo” the content instead of “returning” it.

function load_template_part($template_name, $part_name=null) {
    ob_start();
    get_template_part($template_name, $part_name);
    $var = ob_get_contents();
    ob_end_clean();
    return $var;
}

So, the problem is, when I call woocommerce_show_messages or $woocommerce->show_messages(); from my template part, nothing is showing, or if it is, it shows at the top of the page.

I did try to put the calls inside my shortcode function:

function custom_register_shortcode( $atts, $content ){
    global $woocommerce;
    $woocommerce->show_messages(); 
    $form = load_template_part('framework/views/register-form');
    return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );

Doing so, the message output inside the <head> tag, which is not what I want.

I tried to do the same trick with ob_start(), ob_get_contents() and ob_clean() but nothing would show. The variable would be empty.

I also did try to hook the woocommerce_show_messages to an action as saw in the core:

add_action( 'woocommerce_before_shop_loop', 'woocommerce_show_messages', 10 );

For something like:

add_action( 'before_registration_form', 'woocommerce_show_messages');

And I added this in my template-part:

<?php do_action('before_registration_form'); ?>

But I still can’t manage to get the error messages show inside the box. It would always be inserted in the <head>

I will share final solution when everything is done.

Thanks for your time,

Julien

Related posts

Leave a Reply

3 comments

  1. I finally got this working by hooking a custom function to an action which is called in my header.php

    I guess hooking functions inside template part does not work as intended.

    In header.php, I got this:

    do_action('theme_after_header');

    And here’s the hooked function. Works perfectly.

    function theme_show_messages(){
      woocommerce_show_messages();
    }
    
    add_action('theme_after_header', 'theme_show_messages');
    

    However, I will look into ‘unhooking’ the original show message function since it might show twice. Need to test some more 😉

  2. Replying to a bit of an old question, but you can also try the following:

    $message = apply_filters( 'woocommerce_my_account_message', '' );
    
    if ( ! empty( $message ) ) {
        wc_add_notice( $message );
    }