Answers:
把它放在你的functions.php
文件中:
function wpse_131562_redirect() {
if (
! is_user_logged_in()
&& (is_woocommerce() || is_cart() || is_checkout())
) {
// feel free to customize the following line to suit your needs
wp_redirect(home_url());
exit;
}
}
add_action('template_redirect', 'wpse_131562_redirect');
它有什么作用?
我们检查未登录用户是否要查看WooCommerce页面,并将其重定向到我们的主页。
我只是将重定向更改为
wp_redirect( site_url('my-account/') )
因此,如果未登录或注册,用户将在“我的帐户”页面上重定向。
My Account
页面的最佳方法。最好检索在WooCommerce设置中链接的“我的帐户”页面的永久链接。这样,无论何时更改永久链接,或者您有运行该永久链接的多语言站点,仍然可以正确检索。简而言之:最好不要对永久链接/段(或通常是URL)进行硬编码。更好的方法是:wp_redirect( get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) );
我会改成这个
wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
代替
wp_redirect( site_url('my-account/') );