# Require Login to Browse the Shop

> Sends visitors who are not logged in from the shop, product, cart and checkout pages to the My Account login form, for wholesale and members-only stores.

- Type: PHP
- Runs: Run everywhere
- Category: [Redirects](https://wpide.com/library/category/redirects/)
- Requires: WooCommerce
- Tags: woocommerce, b2b, login
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-require-login-for-shop/

## Code

```php
add_action( 'template_redirect', function () {
    if ( is_user_logged_in() || ! function_exists( 'is_woocommerce' ) || is_account_page() ) {
        return;
    }

    if ( is_woocommerce() || is_cart() || is_checkout() ) {
        wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
        exit;
    }
} );

add_filter( 'woocommerce_login_redirect', function ( $redirect ) {
    $shop = wc_get_page_permalink( 'shop' );

    return $shop ? $shop : $redirect;
}, 10 );
```

## Notes

The My Account page stays open so customers can log in or register. After logging in, customers go back to the shop.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Require Login to Browse the Shop" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
