woocommerce: Show login / register form without ac

2019-06-04 04:19发布

I want to show the woocommerce login and register page but without the options when an user is logged in.

In former versions you could use a shortcode but now you have to use [my_account]. When an user is logged in, he sees his account information.

Are there any hooks / functions to show the register and login forms?

Thanks Maddin

2条回答
在下西门庆
2楼-- · 2019-06-04 05:07

Although Oliver's code works, it's now missing a couple Woocommerce styles and subsequent JS that WP uses to render the form correctly. Perhaps they added these since the answer was posted. This snippet adds relevent <body> classes if the woo-login shortcode is used.

/**
 * Shortcode - Render Woocommerce login/register form
 **/
add_shortcode('woo-login', function() {

    if (is_user_logged_in()) {

    } else {
        ob_start();
        echo '<div class="woocommerce">';
        wc_get_template('myaccount/form-login.php');
        echo '</div>';

        return ob_get_clean();
    }
});


/**
 * Mitigate theme before render
 **/
add_action('template_redirect', function() {

    global $post;

    if (has_shortcode($post->post_content, 'woo-login')) {
        // Add Woocommerce body classes
        add_filter('body_class', function($body_classes) {
            $body_classes[] = 'woocommerce-account';
            $body_classes[] = 'woocommerce-page';

            return $body_classes;
        });
    }
});

Heres a link to the Gist: https://gist.github.com/dhaupin/7a5d429de5be0d623add58edad51b479

查看更多
SAY GOODBYE
3楼-- · 2019-06-04 05:21

i know, the question is a little bit older...

Try this:

add_shortcode('woo-login', 'my_login');

function my_login() {
        if (is_user_logged_in()) {
         // Do what you want...
        } else {
            ob_start();
            echo '<div class="woocommerce">';
            wc_get_template('myaccount/form-login.php');
            echo '</div>';
            return ob_get_clean();
        }
    }
查看更多
登录 后发表回答