Can I use this plugin to password protect woocommerce

Can I use this plugin to password protect woocommerce product categories?

  • Michael Bissett
    • Recruit

    Hey @Wonderdust, hope you're doing well today. :slight_smile:

    Password Protect isn't designed to do this, it's meant to protect content inside a page or a post by means of a shortcode that it uses. I tried looking around, and seeing there was a plugin that could do this for you, but the only one I've found would be the premium version of this plugin:

    https://www.volcanicpixels.com/password-protect-wordpress-plugin/

    Aside from that, the only other way I can think of right now would be to use Membership, but unless you're planning on setting up a membership site, that would be overkill here.

    Let me know if this helps please!

    Regards,
    Michael

  • Wonderdust
    • Site Builder, Child of Zeus

    Hi Michael

    Although this plugin can protect a whole category the password screen doesn't appear until the user has navigated directly to a product. I would like the user to be able to login at category level and then see a whole list of products. I would like access to be open to non members (not based on roles and capabilities) so that I can hand out 1 password to a group of users to be able to access a particular product category. I would be grateful for any pointers as to how to achieve this

    thank you

    Jo

  • Wonderdust
    • Site Builder, Child of Zeus

    Hi Ash

    I've already checked this one out. Unfortunately it doesn't work with custom posts and you can only password protect one category with it.

    Any other suggestions would be gratefully received (although I think I have tried all possible plugins).

    Might this be possible to achieve using .htaccess or some other method?

  • Ash
    • WordPress Hacker

    Hi @Wonderdust

    htaccess could be a solution. Please try the following steps:

    1. Put this in htaccess file:

    # Do the regex check against the URI here, if match, set the "require_auth" var
    SetEnvIf Request_URI ^/product-category/ require_auth=true
    
    # Auth stuff
    AuthUserFile /var/www/htpasswd
    AuthName "Password Protected"
    AuthType Basic
    
    # Setup a deny/allow
    Order Deny,Allow
    # Deny from everyone
    Deny from all
    # except if either of these are satisfied
    Satisfy any
    # 1. a valid authenticated user
    Require valid-user
    # or 2. the "require_auth" var is NOT set
    Allow from env=!require_auth

    Then create a .htpasswd file in /var/www/ directory:

    username:password

    Let us know if it works.

    Cheers
    Ash

  • Wonderdust
    • Site Builder, Child of Zeus

    Hi @ashok

    I have just discovered a problem. I cannot password protect different product categories with different passwords using this method.

    If I login to one category it also gives me access to other categories that are protected with a different login and password.

    I would like to be able to protect each product category with a different login and password.

  • Ash
    • WordPress Hacker

    In that case, you need to have different code for different category. For example, if the category is test:

    # Do the regex check against the URI here, if match, set the "require_auth" var
    SetEnvIf Request_URI ^/product-category/test/ require_auth=true
    
    # Auth stuff
    AuthUserFile /var/www/test/htpasswd
    AuthName "Password Protected"
    AuthType Basic
    
    # Setup a deny/allow
    Order Deny,Allow
    # Deny from everyone
    Deny from all
    # except if either of these are satisfied
    Satisfy any
    # 1. a valid authenticated user
    Require valid-user
    # or 2. the "require_auth" var is NOT set
    Allow from env=!require_auth

    Then create a folder called test in /var/www/ and create a .htpasswd file in /var/www/test/ directory:
    username:password

    Try to do that for multiple categories.

    Note that, you have to add the category name in line no 2 and line no 5 and you also need to create a folder for this.

    First test for two different categories and let me know.

    Cheers
    Ash

  • Wonderdust
    • Site Builder, Child of Zeus

    Ok. So I created a product-category directory inside my wordpress installation and created two directories inside it: test_one and test_two
    Inside each directory I place an .htaccess file with the above code (edited to point to correct .passwd files)

    Then I created two directories in var/www/ named test_one and test_two in which i placed the .passwd files.

    So now when i navigate to product-category/test_one/ or product-category/test_two/ I am prompted for a password but when I enter it I am taken to the actual empty directory that I have created and not the virtual wordpress directory. How can I redirect to the wordpress shop category once I have logged in?

  • Wonderdust
    • Site Builder, Child of Zeus

    Adding the following rewrite rules to the main .htaccess seems to have solved the problem:

    RewriteRule ^product-category/test-one/(.*)$ /index.php?post_type=$1 [PT,L,QSA]
    RewriteRule ^product-category/test-one/*$ /index.php [L,QSA]

    RewriteRule ^product-category/test-two/(.*)$ /index.php?post_type=$1 [PT,L,QSA]
    RewriteRule ^product-category/test-two/*$ /index.php [L,QSA]

  • Wonderdust
    • Site Builder, Child of Zeus

    @Ashok

    I've just realised that because the category/directory is virtual the products within it are not password protected i.e. they can be accessed via their unique urls. Is there a way of preventing this? I also want to make sure that search engines can't access them

    Thank you

    Jo

  • Hoang Ngo
    • Code Slayer

    Hi @Wonderdust,

    I hope you are well today

    Unfortunately, htaccess is not for password protect with complex condition. I suggest we should make some simple plugin for this.

    The idea is we will set rules for each categories, and have ability for on/off the apply for whole products belong to that category.

    And then, we will check if the current request on category/post, if that require password and user never input the password, redirect them to password page.

    That's the idea. Unfortunately this is beyond the scope of custom coding that we could provide. And you could try posting a job on our job board here https://premium.wpmudev.org/wordpress-development/

    Best Regards
    Hoang

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.