禁用未登录用户的结帐页面

时间:2017-02-28 14:05:34

标签: php wordpress logging woocommerce checkout

在WooCommerce中,我正在尝试找到一种方法来禁用非登录用户的woocommerce结帐页面,或者当他们尝试结帐时,他们会被重定向到登录页面。

所以他们应该先登录才能继续结帐。

这可能吗?

由于

1 个答案:

答案 0 :(得分:6)

可以使用以下代码重定向尝试访问结帐的非记录客户:

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

    // Here the conditions (woocommerce checkout page and unlogged user)
    if( is_checkout() && !is_user_logged_in()){

        // Redirecting to your custom login area
        wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );

        // always use exit after wp_redirect() function.
        exit;
    }
}

然后,您可以在购物车页面中显示自定义通知,其中包含一个链接到登录区域的按钮,以避免客户受挫。 最好在之前警告客户,而不是之后。

// Displaying a message on cart page for non logged users (Optional)
add_action( 'woocommerce_before_cart', 'customer_redirected_displaying_message');
function customer_redirected_displaying_message() {
    if( !is_user_logged_in() ){
        // HERE Type your displayed message and text button
        $message = __('To access checkout, you need first to be logged in', 'woocommerce');
        $button_text = __('Login area', 'woocommerce');

        $cart_link = get_permalink( get_option('woocommerce_myaccount_page_id') );

        wc_add_notice(  $message . '<a href="' . $cart_link . '" class="button wc-forward">' . $button_text . '</a>', 'notice' );
    }
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

代码经过测试并有效。