deemi-D-nadeem
deemi-D-nadeem

Reputation: 2514

How to restrict pages to a user in Woocommerce?

I am working on a site and there I am using woocommerce.

If the user logged in or not, they can add to cart a product, go to cart, go to checkout. But now I want that only logged in users can access these pages.

If the guest user click on the add to cart button the message appear You must be logged in. If user go to cart page or checkout page they redirect to register page.

Is there any plugin or hook in Woocommerce?

Upvotes: 1

Views: 2599

Answers (1)

kmandov
kmandov

Reputation: 3209

Found here: https://wordpress.stackexchange.com/questions/131562/make-woocommerce-pages-accessible-for-logged-in-users-only

Put this in your functions.php file:

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');

What does it do? We check if a not-logged-in user wants to see a WooCommerce page, and redirect him/her to our home page.

In your case, you can customize the redirect to the You must be logged page.

Upvotes: 5

Related Questions