How can I hide a product category to non-logged in users

Hi there!

I’m wondering if you can help me with some code… I’m trying to make one product category in MarketPress that is only available / visible to logged in users.

I’ve called the product category ‘Member Products’ with slug ‘member-products’ and I want to find a way that is does not appear anywhere in the site to visitors (widget, store page etc.)

I’ve been playing with the is_user_logged_in request for a while with no luck… help would be much appreciated!

  • RobSamson
    • WPMU DEV Initiate

    Thanks Timothy, I’m actually using Membership already and I’ve seen this plugin before but neither of them recognise the MarketPress Product Categories.

    The easiest way would certainly be if Membership recognised MarketPress product categories and allowed you to make them invisible to logged in / not logged in users through access levels, but that’s not an option at the moment.

    Do you have any other solutions?

  • RobSamson
    • WPMU DEV Initiate

    Thanks Paul, this works to hide the links & pages from visitors. Is it possible to stop visitors from accessing this product category / page with a direct link?

    I’d like to have people redirected to the homepage if they try to access the protected category of products directly without being logged in.

  • Paul
    • The Green Incsub

    @RobSamson, Have updated the gist once more. Added a new function also some other changes.

    At the top of the code you will see the following three lines. The first line was there before. But you need to update this from using the category ID to using the term slug. Works better for other needs. For example on my local test site I have a ‘products’ category I’ve been excluding.

    $mp_excluded_categories = array(‘products’:wink:;

    Next, are some page IDs you can use to redirect the user. The first is for the Archive page. So following my own example of excluding the ‘products’ category the archive page URL is /store/products/category/products/ If the non-user attempts to access the are redirected to the permalink for page 123.

    $mp_redirect_archive_page_id = 123;

    Similar on the Single. If the user is attempting to view a Single product and the product is associated with an excluded product category you can redirect then to a specific page.

    $mp_redirect_single_page_id = 456;

    Let me know your thoughts.

  • RobSamson
    • WPMU DEV Initiate

    Hi Paul, apologies for the slow response I’ve been travelling. Thanks for the additional code, I had been doing it with WP-Membership to block specific pages but this saves the effort – much appreciated. I’ll test it more thoroughly tonight.

    I have an additional question which I’ll post up separately as it’s unrelated – any help you can provide would be appreciated!

    Cheers,

    Rob

  • Timothy
    • Chief Pigeon

    I go away for a few days and look at Paul go! :slight_smile:

    My earlier reference was to Membership URL groups. This allows you to protect any urls within your WordPress site. So you could protect access to product URLs and Category URLS.

    But an option to select categories and protect all child posts/products would be much better, I’d love for that within Membership for all Post Types and taxonomies.

    Anyway, I see Paul has this one covered now. :slight_smile:

    Take care.