WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Hide products of subcategories within main categories (11 posts)

  1. fuchsws
    Member
    Posted 1 year ago #

    I find many people having this issue - but no one seems to have a solution for this. So I try to ask again:

    I want to hide products of subcategories within parent categories.

    This means, when viewing a main/parent category I only want to see products if this single category and not also the products of all containing subcategories.

    How I can I alter the products query in order to get this?

    Might have something to do with the "include_children" parameter of the taxonomy query ... ?! but how/where I can hook into the products list query?

    Thanks in advance!

    http://wordpress.org/extend/plugins/woocommerce/

  2. Sitesforasnip
    Member
    Posted 7 months ago #

    Hi fuchsws - did you ever find a solution for this? I am having the same problem and can't seem to find a solution anywhere. Thanks

  3. fuchsws
    Member
    Posted 7 months ago #

    yes, here you go:

    1) Create a new folder within your theme called "woocommerce" (to be able to overwrite woocommerce-templates)

    2) Copy "wp-content/plugins/woocommerce/templates/archive-product.php" to "wp-content/themes/YOURTHEME/woocommerce/archive-product.php"

    3) at the beginning of your copied "archive-product.php", directly after "get_header('shop');" add:

    global $wp_query; $term = get_term_by( 'slug', get_query_var($wp_query->query_vars['taxonomy']), $wp_query->query_vars['taxonomy']);

    => This should than only list products within the current category and not any products of subcategories!

  4. Sitesforasnip
    Member
    Posted 7 months ago #

    Many thanks I'll give this a go!

  5. Sitesforasnip
    Member
    Posted 7 months ago #

    Hi,

    Many thanks for your help. Unfortunately I can't seem to get this to work I have a category called individual products and a subcategory of this called career advice. For some reason I'm still seeing the two career advice products also showing up in individual products:

    http://www.smartjobsolutions.co.uk/product-category/individual-products/
    http://www.smartjobsolutions.co.uk/product-category/individual-products/career-advice/

    I have added the code as above to my new archive-product.php file so that it looks like:

    <?php
    /**
     * The Template for displaying product archives, including the main shop page which is a post type archive.
     *
     * Override this template by copying it to yourtheme/woocommerce/archive-product.php
     *
     * @author 		WooThemes
     * @package 	WooCommerce/Templates
     * @version     2.0.0
     */
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    get_header('shop');
    
    global $wp_query; $term = get_term_by( 'slug', get_query_var($wp_query->query_vars['taxonomy']), $wp_query->query_vars['taxonomy']);
    ?>
    
    	<?php
    		/**
    		 * woocommerce_before_main_content hook
    		 *
    		 * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
    		 * @hooked woocommerce_breadcrumb - 20
    		 */
    		do_action('woocommerce_before_main_content');
    	?>
    
    		<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
    
    			<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
    
    		<?php endif; ?>
    
    		<?php do_action( 'woocommerce_archive_description' ); ?>
    
    		<?php if ( have_posts() ) : ?>
    
    			<?php
    				/**
    				 * woocommerce_before_shop_loop hook
    				 *
    				 * @hooked woocommerce_result_count - 20
    				 * @hooked woocommerce_catalog_ordering - 30
    				 */
    				do_action( 'woocommerce_before_shop_loop' );
    			?>
    
    			<?php woocommerce_product_loop_start(); ?>
    
    				<?php woocommerce_product_subcategories(); ?>
    
    				<?php while ( have_posts() ) : the_post(); ?>
    
    					<?php woocommerce_get_template_part( 'content', 'product' ); ?>
    
    				<?php endwhile; // end of the loop. ?>
    
    			<?php woocommerce_product_loop_end(); ?>
    
    			<?php
    				/**
    				 * woocommerce_after_shop_loop hook
    				 *
    				 * @hooked woocommerce_pagination - 10
    				 */
    				do_action( 'woocommerce_after_shop_loop' );
    			?>
    
    		<?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>
    
    			<?php woocommerce_get_template( 'loop/no-products-found.php' ); ?>
    
    		<?php endif; ?>
    
    	<?php
    		/**
    		 * woocommerce_after_main_content hook
    		 *
    		 * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
    		 */
    		do_action('woocommerce_after_main_content');
    	?>
    
    	<?php
    		/**
    		 * woocommerce_sidebar hook
    		 *
    		 * @hooked woocommerce_get_sidebar - 10
    		 */
    		do_action('woocommerce_sidebar');
    	?>
    
    <?php get_footer('shop'); ?>

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

    Any thoughts would be greatly appreciated. Thanks

  6. fuchsws
    Member
    Posted 7 months ago #

    ya correct, I forgot a snippet (it was a while ago I figured this out) ...

    so in your local copy of "archive-product.php" go to

    <?php woocommerce_product_loop_start(); ?>
    <?php woocommerce_product_subcategories(); ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php woocommerce_get_template_part( 'content', 'product' ); ?>
    <?php endwhile; // end of the loop. ?>
    <?php woocommerce_product_loop_end(); ?>

    And replace by:

    <?php woocommerce_product_loop_start(); ?>
    <?php woocommerce_product_subcategories(); ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php $terms = wp_get_post_terms(get_the_ID(), $wp_query->query_vars['taxonomy']);
    if ($terms[0]->term_id == $term->term_id): ?>
    <?php woocommerce_get_template_part( 'content', 'product' ); ?>
    <?php endif; endwhile; // end of the loop. ?>
    <?php woocommerce_product_loop_end(); ?>

    Hopefully that's all ...

  7. Sitesforasnip
    Member
    Posted 7 months ago #

    Absolutely terrific - works a charm. Thank you so much you're a life saver!

  8. phisphere
    Member
    Posted 7 months ago #

    Hi,

    I was able to hide products from subcategories from the parent category, but the paginator and product count remain unaffected by the change, so they the count still counts in the products from the subcategories and pagination displays all pages as before (showing a lot of empty pages).

    The dirty fix is to display all products by default but is it possible to make product count and pagination understand the change in the product display?

    Best Regards

  9. atulsohan
    Member
    Posted 2 months ago #

    how did sitesforasnip do it in his example website iam using
    wp:3.8.1 and wc:2.1.6
    he seems to have made it proper
    please advise

  10. Sitesforasnip
    Member
    Posted 2 months ago #

    Hi there,

    I think I just worked around it by changing the number of products displayed per page so that they would all fit on one page the I hid the product count with some CSS. Something like this to change the number of products per page:

    // Display 24 products per page. Goes in functions.php
    add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 24;' ), 20 );

  11. dmonday
    Member
    Posted 2 months ago #

    I'm also having this issue, any fix? Above doesn't work for me.

    Thanks, Dan

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic