Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Woocommerce-隐藏未注册用户的店铺_Php_Wordpress_Woocommerce - Fatal编程技术网

Php Woocommerce-隐藏未注册用户的店铺

Php Woocommerce-隐藏未注册用户的店铺,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我有一个woocommerce网站,我想在用户未登录时隐藏商店。我把这个代码放在文件里了!archive-product.php,它位于woocommerce文件夹中的我的模板“Twenty12 child”中 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly auth_redirect(); get_header( 'shop' ); ?> 通常,“auth_redirect()”必须在登录页面中重定向

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

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;
}

我做错什么了吗?

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

function custom_redirect() {        
    if( is_shop() && ! is_user_logged_in() ) {
        wp_redirect( home_url() ); 
        exit();
    }   
}

add_action("template_redirect","custom_redirect");

谢谢你。我还添加了一些其他功能,这些功能可能会有所帮助

// 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");