• Resolved TechSolutions

    (@ancientbarrage)


    Really hope someone can help with this. I have several regular product categories, and one which is for Adult Toys, which requires an age verification to enter and view the products.

    The problem is, I need the products for that category to stay only within that category’s page, behind the age verification, but they still show on the shop front page in Woocommerce’s ‘All Products’ section. I don’t want to give up the All Products section as it’s useful for people browsing, and I’ve tried installing two plugins that don’t work for hiding products and categories from the All Products front end (because they also remove them from the Category Page, which makes them useless for this setup).

    I’m desperate for a solution, can anyone help? Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • When editing a product you can select the right option under “Catalog visibility”. Probably select hidden and then create a page and include that product on it.

    Thread Starter TechSolutions

    (@ancientbarrage)

    Hi, thanks so much for your reply. When selecting “Hidden” it just hides the product from displaying on the shop in any circumstance. The URL is still active, but it removes it from not only the ‘All Products’ list, but also the category page where it should remain visible.

    The idea here is only to remove from the ‘All Products’ list so the products within the Adult category remain there, protected by age verification.

    Hello there,

    To hide products in specific categories from the “All Products” section in WooCommerce while keeping them visible on their respective category pages, you can achieve this with some custom code. Here’s a high-level approach to solve this problem:

    1. Child Theme: If you’re not already using a child theme for your WordPress site, create one. This is important to ensure your changes are not lost when you update your theme.
    2. Custom Functions: In your child theme’s functions.php file, you can add custom functions to filter the products that appear in the “All Products” section.
    3. Use the pre_get_posts Hook: You can use the pre_get_posts hook to modify the query that retrieves products. Here’s an example of how you can do this to exclude certain categories:
    add_action('pre_get_posts', 'exclude_categories_from_all_products');
    
    function exclude_categories_from_all_products($query) {
           if (is_admin() || !$query->is_main_query()) {
               return;
           }
    
           if (is_shop() || is_product_category()) {
               $excluded_categories = array(1, 2, 3); // Replace with the IDs of the categories you want to exclude
               $query->set('tax_query', array(
                   array(
                       'taxonomy' => 'product_cat',
                       'field' => 'id',
                       'terms' => $excluded_categories,
                       'operator' => 'NOT IN',
                   ),
               ));
           }
       }

    Replace 1, 2, 3 with the actual category IDs you want to exclude.

    1. Save and Test: After adding this code, save your functions.php file and test it. Products from the specified categories should no longer appear on the “All Products” page while remaining visible on their respective category pages.

    Remember to back up your site or test this in a staging environment before implementing it on a live site to ensure it works as expected. Also, be cautious when modifying code to avoid any syntax errors.

    Thread Starter TechSolutions

    (@ancientbarrage)

    Thank you for your reply! This code does indeed work. It can also be applied using Code Snippets plugin which won’t be overwritten by theme updates. Much appreciated.

    Hi @ancientbarrage

    We’re glad that the suggestion worked for you!

    I’m going to mark this thread as resolved, but please don’t hesitate to start a new topic if you have any more questions down the line.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[NSFW] Need to hide certain items from ‘ALL PRODUCTS’ but NOT category page’ is closed to new replies.