Woocommerce-隐藏未注册用户的商店


Woocommerce - Hide shop for unregistered user

我有一个wooccommerce网站,我想在用户未登录时隐藏商店。我把这个代码放在文件里了!archive-product.php,它在我的模板"twentytwelve child"中的wooccommerce文件夹中。

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
auth_redirect();
get_header( 'shop' ); ?>

通常情况下,"auth_redirect()"必须在登录页面中重定向我,但它根本不起作用。

我也尝试过使用此代码,但它也不起作用。

$login = is_user_logged_in();
if ($login == FALSE ) {
    wp_redirect( home_url() ); 
    exit;
}

我做错什么了吗?

谢谢。我还添加了一些其他有用的功能。

// Redirect none registered users to a login page
function custom_redirect() {        
    if( (is_shop() || is_product() || is_product_category() )  && ! is_user_logged_in() ) {
        wp_redirect( site_url( '/mon-compte' ) );
        exit();
    }   
}
add_action("template_redirect","custom_redirect");

您不需要为您想要实现的目标修改Wooccommerce模板文件。只需将以下代码添加到functions.php

function custom_redirect() {        
    if( is_shop() && ! is_user_logged_in() ) {
        wp_redirect( home_url() ); 
        exit();
    }   
}
add_action("template_redirect","custom_redirect");