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

Disable checkout page for not logged in users

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

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

这可能吗?

谢谢

可以使用此代码重定向尝试访问结帐的未登录客户:

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 文件或任何插件文件。

代码已经过测试并且有效。