How to hide the WooCommerce Shop page

I’ve been asked if it is possible to hide the WooCommerce Shop page based on Groups’ access restrictions. As it is, trying to apply the normal way (checking a capability in the Access restrictions of the page) simply doesn’t work.

So what does work? Here’s an example:

Let’s assume that you are protecting your pages using the standard groups_read_post capability, any user that belongs to a group with that capability should be able to access the Shop page.

Create the woocommerce folder under the base folder of your active theme and copy archive-product.php from woocommerce/templates there.

At the top of your fresh copy, insert:

if ( class_exists( 'Groups_User' ) ) {
	$redirect = true;
	if ( $user_id = get_current_user_id() ) {
		$user = new Groups_User( $user_id );
		if ( $user->can( 'groups_read_post' ) ) {
			$redirect = false;
		}
	}
	if ( $redirect ) {
		wp_redirect( get_bloginfo( 'home' ) );
		exit;
	}
}

… right before:

get_header('shop');

This will redirect visitors who try to access the Shop page to the site’s home page.


Comments

53 responses to “How to hide the WooCommerce Shop page”

  1. hi can you please help me with this:
    I have created a page with 2 columns, one for showing the subcategories i want and the other column to show the products of the subcategory one chooses, the problem is that i can not get it to list the products on my 2nd column, instead woocommerce lists them on its product archive page, can you please help me what shortcode shall i use in the column i would like the products to appear as someone clicks on the subcategories?
    Thank you

    1. antonio Avatar
      antonio

      Hi Kostas,
      In this documentation page you have available our shortcodes.
      This is not a question about our plugins, but you could add an extra param on your left links, something like ?cate=xxx, and using the ‘woocommerce_shortcode_products_query’ filter, you can get the param, and add this category value to the shortcode params. I think that this post can help you as reference, and using $_GET
      Kind Regards,
      Antonio B.

  2. Antonio,

    Is this method still working after the latest update? I can’t seem to hide the /shop page from logged-out users.

    Right now, I have a Groups setup named Approved and a capability applied to that group also called “Approved”. I added the “Approved” capability to the necessary pages (shop, cart, checkout). But it’s not working for the shop page.

    I modified the line:

    if ( $user->can( ‘groups_read_post’ ) ) {

    to read:

    if ( $user->can( ‘Approved’ ) ) {

    Other than that, everything is exactly as your original post, but it doesn’t appear to be working. When logged out user navigates to the /shop page, they can still see the page.

    Any ideas?

    1. Hi Dennon,
      Yes, it should work fine. You can share your full code using pastebin.com
      Kind regards,
      Antonio B.

      1. Antonio,

        This is everything through the line get_header(‘shop’);… http://pastebin.com/MWhwWNMb

        But I believe the issue lies in the way the theme I’m using is setup. I found a note in the WooCommerce documentation (Noted here: https://docs.woothemes.com/document/template-structure/) which states you’ll be unable to override archive-product.php if your theme uses a custom woocommerce.php file.

        I’ve tried this a lot of different ways – even adding it directly to the core archive-product.php file – and I can’t get any of the changes to take place. So you can see it, this is the current version of the woocommerce.php file that my theme is using… http://pastebin.com/BupJTptW

        Any help would be great!

        1. Hi Dennon,
          It seems the code works fine. You can try to add this code before the get_header() in your woocommerce.php theme file.
          If this doesn’t work, add this code in your-theme/woocommerce/archive-product.php if the file exists.
          Kind Regards,
          Antonio B.

  3. Hello Antonio,

    Thanks for your reply. I also tried Groups 404 redirect but it didn’t work for me because it seems the ‘Shop’ page on WooCommerce do not show a 404 error but an empty page in this case. It’s seems to be by design.

    Nevertheless, I could achieve my goal with another trick that I publish here, maybe it can help some readers.
    I added a function in the functions.php page to check if a user is logged in when he tries to access any WooCommerce main page (Shop, Cart, Checkout); if he is not legged in, he is automatically redirected to the “My Account” page so he can register or log in.

    function wp_wc_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( site_url(‘my-account/’) );
    exit;
    }
    }
    add_action(‘template_redirect’, ‘wp_wc_redirect’);

    It works perfectly.

    Thanks for all,

    Pierre

    1. Thanks for sharing.
      Kind Regards

  4. Hello,

    I use Groups for Woocommerce which seems wonderful, but as I’m not a code expert, I’m a little bit stuck.

    I read all this interesting thread because I also need to redirect people who wants to acces the products page but are not (yet) logged in; they should be redirected to the ‘my-account’ page so they can log in or register, and then after all the beauty of the Groups plugin can act.

    So I:
    – created the woocommerce directory at the theme root (I use Zerif-Pro theme)
    – copied there the archive-product.php file
    – edited this file as described above by Kento

    Nothing works. It seems that the archive-product.php file is never read by woocommerce, in fact, because I tried to modify a lot of things in this file for tests purpose (including breaking the code): nothing happens.

    I surely miss something but what ? Someone to bring some light to my poor lantern?

    Thanks

    Pierre

    1. Hi Pierre,
      you can try to restrict your products page, and use Groups 404 redirect plugin for redirect the users to your-acount page.
      Kind Regards,
      Antonio B.

  5. Hi, how can i hide and other woocommerce pages(cart, checkout)?Thank you in advance

    1. Hi Petros,
      this post can help you to get the woocommerce pages. So using the above code and changing the url you can do it.
      Kind Regards,
      Antonio B.

  6. How can I hide individual products in a woocommerce store from users that have logged on, i.e. users that have already been assigned to a group . I’ve been searching for a solution on google for two days now, but cannot seem to find anything on this.

    1. Hi Gavin,
      if you need to hide the product, you can use the Groups plugin. If you only need to set purchasable or not according to the role, you can use the Woocommerce Role Purchasable plugin.
      Kind regards,
      Antonio B.

  7. Hi Dogal,

    for me this works very well

    if ( $redirect ) {
    wp_redirect( get_bloginfo( ‘home’ ) );
    exit;
    }

    but i need to redirect to the page “product”. If i replace home with product, it does not work.
    si i fill in the hole url. This is not what i need.

    I need a varable solution, because, if i go live to another server, i need to change the whole url.

    greetings Harry

    1. Hi Harry,
      maybe this post can help you: https://www.skyverge.com/blog/get-woocommerce-page-urls/
      Kind regards,
      Antonio B.

  8. Daniel Gutierrez Avatar
    Daniel Gutierrez

    THx Kento, this worked great!

  9. Hi,
    I have a question: I want to remove the shop base page completely because I am only selling one product and don’t need the default shop base page which shows product archive etc. I have deleted the [woocommerce_shop] page and also deleted this page from my menu(instead I added the product which I am selling to the menu, naming it “order”), but still, if you type in my site’s name and then /shop, you get directed to the default shop base page.
    How can I remove this page completely or at least hide it?

    Thank you,
    Dogal

    1. Hi Dogal,

      You mean you have actually deleted the page from your admin side and it still shows up? Doesn’t sound likely 🙂 You probably need to make sure by going to Pages on your WordPress Dashboard and delete the page from there. If you have just removed the setting from WooCommerce, it doesn’t mean the page itself is deleted.

      Cheers

      1. Hi Kento,
        yes, I DID delete the shop page from the admin board COMPLETELY and it still shows up when you type in the URL …/shop!!
        I know it sounds weird but I tried it on two test pages with the same result. I mean, a usual customer would not search for a specific url but instead use my menu, but the URL /shop is not that uncommon to guess or look for and I don’t want the product archive to show up…the only thing I can think of how to avoid this is creating a page and calling it some strange name like hsgfrhjunsbu6675673 and setting the Woocommerce base shop page to this. Then you get the 404 page when you type in …/shop. But there must be a proper way?

        1. Hi Dogal,

          WooCommerce defines the product custom post type and uses ‘shop’ as the value for register_post_type()‘s ‘has_archive’ parameter which produces the product listing you get when you visit that …/shop URL even though the page isn’t there.

          You can simply apply the approach outlined on this post using the customized template and Groups to redirect to e.g. the home page of your site as described here.

          Cheers

  10. Everything works great with the code below, but I would like to redirect the user to the Woocommerce “My Account” page instead of the WordPress login. I have tried different solutions but can’t get my head around it.

    $current_url = ( is_ssl() ? ‘https://’ : ‘http://’ ) . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’];
    wp_redirect( wp_login_url( $current_url ) );
    exit;

    1. Hi Henrik,

      Wouldn’t it be easier to simply indicate the My Account page’s URL as the parameter to wp_redirect() ?

      1. Yes that worked! I wasn’t thinking clearly. Thanks for the support!

  11. I am using WordPress Multisite and want to hide the shop page in one of the client sites…. multisite doesn’t allow you to play around with theme directories for the individual client sites (that I can figure out anyway!). So any ideas how I can do this restriction without your suggestion in the blog post above?

    1. Hi,
      you can check the current site using get_current_blog_id function.
      cheers

  12. Hi Kento,
    We’re using the Groups plugin with WooCommerce. Is it possible to ‘hide’ a product from a registered group, instead of the opposite?
    So, we want to set up a free membership as a product. Once they join, the ‘membership’ product should then be hidden from members of that group. But it is still visible to everyone else.
    We want it this way as free membership also comes with a free product, so members can only join once.

    1. Hi Howard,

      You would need to implement a customized solution for that, it can use Groups’ API and similar to what is explained in Exclude products from a particular category on the shop page, if you use a category for these and also check if the user belongs to the product’s group, then you should be able to achieve that effect.

      Does that help?

  13. How would I hide the woocommerce shop page based on affiliate log in? I would like the shop page to be seen only by affiliates who are logged in. I am using Affiliates Enterprise and Woocommerce. Thanks.

    1. Hi Thomas,

      You can use the method outlined on How to hide the WooCommerce Shop Page and use the affiliates_user_is_affiliate() function as the conditional.

      Cheers

  14. Hi. My comment appears to have got deleted. I was just wondering how to redirect to the may account instead of wp_redirect( get_bloginfo( ‘home’ ) );

    Thanks

    1. Hi Dan, probably an over-enthusiastic spam filter ate it 😉

      Do you mean something like this … ?


      $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
      wp_redirect( wp_login_url( $current_url ) );
      exit;

      1. Many thanks for taking to respond Kento. That works great.

  15. ok I have found some code that will redirect to the shop after login

    add_filter(‘woocommerce_login_redirect’, ‘ras_login_redirect’);

    function ras_login_redirect( $redirect_to ) {
    $redirect_to = “www.yourshop.com”;
    return $redirect_to;
    }

    but the only thing I need know is how to direct the user to the login (my-account) when they click on shop on the homepage instead of being redirected back to the homepage – I’ve tried a few variables but they do not work as I’m not much of a coder – really appreciate your help on this one

    1. I would rather use Groups 404 Redirect or Peter’s Login Redirect, no coding involved 😉 See http://www.itthinx.com/useful-plugins/

  16. Hi Kento
    Many thanks for explaining this – I have manage to block the shop page and redirect to the home page but is there any chance you could explain that instead of redirecting to the home page how to redirect to the login page and then to the shop? I would really appreciate your help. Thanks

    1. Wouldn’t Groups 404 Redirect be suitable for that?

  17. Hi Kento,

    Thank you for your response.
    I understand.
    In the meantime, I decided that the non-registration customers will added to something new group after registration.

    Regards

  18. Hello

    I am using WooCommerce, Groups, Groups WooCommerce and Affiliates Enterprise.
    I think these are fantastic plugins!
    I want to know how to set a specific product is not shown to the Premium group.

    My client has a product for Non-registration customers.(ex.Product A)
    And also, client has product for Premium group members.(ex.Product B)
    It is the scenario which is promoted to a Premium group member when the Non-registration customer buys product A.

    [Visibility Restriction Case 1]
    Non-registration customer can see Product A.
    Premium group member can not see Product A.

    [Visibility Restriction Case 2]
    Premium group member can see Product B.
    Non-registration customer can not see Product B.

    Case 2 is easy to setup using “groups_read_post” capability.
    But I could not set Case 1.
    I have searched for Forum, but I did not find useful information.
    I feel it can be set if there is something like “groups_unread_post”….

    Please advise how to set Restriction Case 1.

    Thank you.

    1. Hi Masayuki,

      Many thanks for the feedback on the plugins and for using them 🙂

      For the case you have explained, one way is to use product-related capabilities assigned to product-specific groups.

      For example:

      Product “A” – Group “A” – capability “a”
      Product “B” – Group “B” – capability “b”

      Then you can distinguish more precisely who can see what based on group membership. The only thing that this doesn’t cover, is hiding Product A from the Premium group members. This is something that you could build into the template but you can’t achieve it using the back end UI elements.

      Cheers

  19. Bernhard Avatar

    Hello,

    I am also using groups access restrictions in order to realize a member’s only section on my page. Everything works fine, but the woocommerce categories are still shown. No products are shown for registered members, but all categories.

    How can we hide the categories as well?

    thx for your help,
    Bernhard

    1. Hi Bernhard,

      This is something I’m currently looking at – restricting categories based on groups. It might be made part of the core plugin or a separate extension, depending on the impact and complexity.

      One approach you could use currently is to adjust the templates used to display products in categories and the categories themselves – Groups’ API can be used to check the membership and display them conditionally.

      I hope that helps at least to point you in the right direction.

      Cheers

      1. Hi Kento,

        thx for your reply. I think it could help if I would be clever enough to build it into the template like you suggested. Sadly that is beyond my scope..

        So I do hope this feature becomes available as plugin feature soon..

        cheers,
        Bernhard

        1. Thanks for the feedback and the suggestion Bernhard, yes that would require adequate knowledge on how to code it into the template. It might be an option with advanced logic built into the plugin, but this hasn’t been done yet.

  20. Thanks Antonio, this works absolutely brilliant!

  21. Great stuff! Does this or another approach with the groups plugin work for single products? I have tried many plugins for having members-only products, restricting access to the product and removing it from catalog etc.

    Thank you for any input or direction. 🙂

    1. Hi,
      yes, you can restrict access to products.

      1. Hi Antonio, how i can hide products in shop page if user without registration

        Thanks

        1. Hi Valeriy,

          For example, add the groups_read_post capability to the Registered group. Then use the Access restrictions box – add groups_read_post under Enforce read access – on those products you want to hide from unregistered visitors.

          1. kento hi, i need use plugin?

            1. Only Groups 🙂

              1. Hi, after adding this capability to products, is there anyway to redirect the product to login page instead of hiding them?

                1. Hi, yes, instead of wp_redirect( get_bloginfo( 'home' ) ); you can use:

                  $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                  wp_redirect( wp_login_url( $current_url ) );

                  This would also return to the shop page after login.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share