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

时间:2017-04-26 16:40:02

标签: php wordpress redirect woocommerce logged

在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();
        }
    }
?>

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

我该如何解决这个问题。

由于

1 个答案:

答案 0 :(得分:1)

您应该尝试将代码嵌入到隐藏在 template_redirect 操作挂钩中的自定义函数中:

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上测试并正常工作