如果未登录,将 WooCommerce 页面重定向到“我的帐户”页面(问题)

问题描述 投票:0回答:2

在 WooCommerce 中,当未登录的用户访问任何 WooCommerce 页面时,我会将其重定向到“我的帐户”页面。但是,在用户登录后(在“我的帐户”页面中),当我尝试访问同一个 woocommerce 页面时,它会将我重定向到“我的帐户”页面。 但当我访问任何其他 WooCommerce 页面时,它工作得很好。 我正在使用此代码来实现此目的:
<?php if ( !is_user_logged_in() ) { if(is_woocommerce() || is_shop() || is_cart() || is_checkout()) { wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id')) ); exit(); } } ?>

我认为问题在于它在缓存中存储了某些内容或另一个重定向问题,因此当用户访问相同的网址时,它会在登录时重定向到

我的帐户

如何解决这个问题?

您应该尝试将代码嵌入到挂钩于

php wordpress http-redirect woocommerce
2个回答
3
投票

操作挂钩中的自定义函数中:

add_action( 'template_redirect', 'wc_redirect_non_logged_to_login_access');
function wc_redirect_non_logged_to_login_access() {

    if ( !is_user_logged_in() && ( is_woocommerce() || is_shop() || is_cart() || is_checkout() ) ) {
        wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id')) );
        exit();
    }
}

此代码位于活动子主题(或主题)的 function.php 文件中,或者也位于任何插件文件中。


此代码在 WooCommerce v2.6.x + v3.0.x 上进行了测试并且可以工作

<?php if ( !is_user_logged_in() ) { if(is_woocommerce() || is_shop() || is_cart() || is_checkout()) { wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id')) ); exit(); } } elseif ( is_page( 'my-account' ) && is_user_logged_in() ) { // Redirect logged-in users from the My Account page to another page if needed // Replace 'your-custom-page-slug' with the slug of the page you want to redirect logged-in users to wp_redirect( get_permalink( get_page_by_path( 'your-page-slug' ) ) ); exit(); }


0
投票
检查此代码:用户是否已登录以及他们是否正在尝试访问“我的帐户”页面。如果满足这两个条件,您可以根据需要将它们重定向到另一个页面。将“your-page-slug”替换为您想要从“我的帐户”页面重定向登录用户的页面的 slug。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.