Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1491703

    Hi there!

    I have a range of products listed in our e-com store that are restricted by specific user IDs. To do this, we use a plugin called Members Only.

    These products are private and cannot be purchased or viewed by the general public – only specific logged in users can see their specific custom products. To a general user browsing our website, these products do not display in the shop (which is correct). However, if someone was to use the search bar for an item (e.g. “Flag”), these private, restricted products display in the search dropdown.

    Search result dropdown, logged out user:
    https://www.dropbox.com/scl/fi/590zcjp3uqnwnlsk8oro9/ClientPortal_LoggedOut_Flag.png?rlkey=xvg6qvjiuxf7o6w0tinu7s0tq&dl=0

    When you click “view all results”, these products don’t display in the results page – so it’s only in the dropdown itself.

    These products are set to not display in the search results, as a failsafe:
    https://www.dropbox.com/scl/fi/0fjw5836a5tbzzuwky6hn/ClientPortal_SuperButcherFlag_ShopOnly.png?rlkey=mnxq6izhmmw1x4ra5y3c1bbd9&dl=0

    Yet they’re still displaying.

    I’ve reached out to Plugin Republic, the dev for Members Only, and they’ve said it’s a theme issue:

    https://www.dropbox.com/scl/fi/lt25qg849jq9r4onnvntl/ClientPortal_PluginRepublicReply.png?rlkey=iw9xyh1dfv3dq4muzqfmhmmtk&dl=0

    Is there a filter they could use to check if the product is restricted before it gets displayed in the search results dropdown please?

    Thank you in advance for your help :)

    #1491707

    Hey sarahd167,

    Thank you for the inquiry.

    You may need to use the avf_ajax_search_query filter to adjust how the items in the search results are retrieved. Please forward this filter to the plugin developers — they will know the correct query to use.

    Best regards,
    Ismael

    #1491711

    Hey Ismael,

    Amazing, thank you very much! I’ve forwarded that onto the plugin developers. Fingers crossed!

    I appreciate your help, thank you!

    Many thanks,
    Sarah

    #1491721

    Hi,

    Please keep us posted. We’ll keep the thread open.

    Best regards,
    Ismael

    #1491769

    there are some Plugins that will offer such a function: CatalogX (Freemium) or WooCommerce Catalog Mode
    or you might use code snippets – but it is not as easy as it sounds. There are still backdoors that can be used to find something.

    but this might give you a chance to exclude some Products by slugs ( f.e.: members-only or premium)

    This is what an AI like Claude would give to you:

    // 1. Hide from queries
    add_action('pre_get_posts', 'hide_member_products_from_guests');
    function hide_member_products_from_guests($query) {
        if (is_admin() || !$query->is_main_query()) {
            return;
        }
        
        if (!is_user_logged_in()) {
            $tax_query = $query->get('tax_query') ?: array();
            
            $tax_query[] = array(
                'taxonomy' => 'product_tag',  // product_cat → product_tag
                'field'    => 'slug',
                'terms'    => array('members-only', 'premium'),  // Deine Tag-Slugs
                'operator' => 'NOT IN'
            );
            
            $query->set('tax_query', $tax_query);
        }
    }
    
    // 2. Visibility Filter
    add_filter('woocommerce_product_is_visible', 'filter_member_products_visibility', 10, 2);
    function filter_member_products_visibility($visible, $product_id) {
        if (!is_user_logged_in()) {
            if (has_term('members-only', 'product_tag', $product_id)) {  // product_cat → product_tag
                return false;
            }
        }
        return $visible;
    }
    
    // 3. Block direct access
    add_action('template_redirect', 'block_member_product_access');
    function block_member_product_access() {
        if (!is_user_logged_in() && is_product()) {
            global $post;
            if (has_term('members-only', 'product_tag', $post->ID)) {  // product_cat → product_tag
                wp_redirect(wp_login_url(get_permalink()));
                exit;
            }
        }
    }
    
    // 4. Exclude from search (maybe an ajax pendent is needed too)
    add_filter('posts_where', 'exclude_member_products_from_search', 10, 2);
    function exclude_member_products_from_search($where, $query) {
        global $wpdb;
        
        if (!is_admin() && $query->is_search() && !is_user_logged_in()) {
            $where .= " AND {$wpdb->posts}.ID NOT IN (
                SELECT object_id FROM {$wpdb->term_relationships}
                INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id
                INNER JOIN {$wpdb->terms} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id
                WHERE {$wpdb->terms}.slug IN ('members-only', 'premium')  // Deine Tags hier
                AND {$wpdb->term_taxonomy}.taxonomy = 'product_tag'  // product_cat → product_tag
            )";
        }
        
        return $where;
    }
    
    // 5. Exclude from REST API 
    add_filter('woocommerce_rest_prepare_product_object', 'hide_member_products_from_api', 10, 3);
    function hide_member_products_from_api($response, $object, $request) {
        if (!is_user_logged_in() && has_term('members-only', 'product_tag', $object->get_id())) {  // product_cat → product_tag
            return new WP_Error('rest_forbidden', 'Zugriff verweigert', array('status' => 403));
        }
        return $response;
    }
    #1491770

    PS : If these products are assigned to a category that has the members-only function, then the code will of course also work with category exclusion.
    just see the comments on the snippets with product_tag versus product_cat

    for ajax search it might be neccessary to have:

    // ENFOLD AJAX-SEARCH: exclude members-only slug
    add_filter('avf_ajax_search_query', 'exclude_member_products_from_enfold_ajax', 10, 1);
    function exclude_member_products_from_enfold_ajax($search_parameters) {
        // Only for users who are not logged in
        if (!is_user_logged_in()) {
            // Parse parameters
            parse_str($search_parameters, $params);
            
            // Add Tax Query
            $params['tax_query'] = array(
                array(
                    'taxonomy' => 'product_tag',  // or 'product_cat'
                    'field'    => 'slug',
                    'terms'    => array('members-only', 'premium'),
                    'operator' => 'NOT IN'
                )
            );     
            // Convert back to string
            $search_parameters = http_build_query($params);
        }
        return $search_parameters;
    }

    Unfortunately, I can’t test it myself, as I’m not currently managing any shop websites.

    #1491859

    Hi Ismael,

    The plugin developers used the filter you suggested – they’ve sent me through a code snippet with it and it has worked perfectly. Case closed!

    Thank you very much for your help! :)

    Many thanks,
    Sarah

    #1491863

    And you don’t like to make it public? Perhaps other participants could also benefit from this.
    Is it nearby my approach on: https://kriesi.at/support/topic/restricted-product-displaying-in-search-results/#post-1491770

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.