Categories that have subcategories display products from subcategories

This topic has 6 replies, 2 voices, and was last updated 1 months ago ago by Luca Rossi

  • Avatar: Goostaf
    Goostaf
    Participant
    March 13, 2025 at 18:53

    Hello, I do not remember, but it seems you helped with this once hook, I have in categories where there are subcategories products are displayed randomly, I want to see my products here, how to fix it? For example: there is a category: Balloon Compositions and inside there are subcategories: Fountains, in Figures, so these products are chaotically displayed in the parent category, although they are not needed there, I want to see here only those products that I mark in the category: Compositions.

    Please, contact administrator
    for this information.
    Files is visible for topic creator and
    support staff only.
    5 Answers
    Avatar: Justin
    Luca Rossi
    Support staff
    March 15, 2025 at 10:01

    Dear @Goostaf,

    We would appreciate it if you could provide us with more details regarding your concerns, as we are not entirely clear on the issues you are experiencing.

    Could you kindly elaborate further?

    Looking forward to your response.

    Best regards,
    The 8Theme Team

    Avatar: Goostaf
    Goostaf
    Participant
    March 15, 2025 at 11:07

    Here is my thread here, you have given me a solution, but it doesn’t work for some reason, I put the hook in the function: https://www.8theme.com/topic/how-do-categories-with-subcategories-display-only-the-desired-products/#post-418254.

    Avatar: Justin
    Luca Rossi
    Support staff
    March 17, 2025 at 09:56

    Dear @Goostaf,

    Could you kindly provide your custom code? This will allow us to review it more thoroughly.

    Thank you for your cooperation.

    **Best regards,**
    8Theme Team

    Avatar: Goostaf
    Goostaf
    Participant
    March 17, 2025 at 13:07

    That’s how you had a skinned man in there:

    function change_parent_category_query($query) {
    if( ! is_admin() && $query->query_vars[‘post_type’] == ‘product’) {

    global $wpdb;
    $parent_id = get_queried_object()->term_id;

    if( isset($parent_id) && is_numeric($parent_id) ){
    $all_subcategory_ids = $wpdb->get_results( $wpdb->prepare( “SELECT term_taxonomy_id FROM {$wpdb->prefix}term_taxonomy WHERE taxonomy = ‘product_cat’ AND parent = %d”, $parent_id ) );

    $list_ids = array();
    foreach( $all_subcategory_ids as $asi ){
    $list_ids[] = esc_attr( $asi->term_taxonomy_id );
    }

    $tax_query = $query->get( ‘tax_query’ );

    $tax_query[] = array(
    ‘taxonomy’ => ‘product_cat’,
    ‘field’ => ‘term_id’,
    ‘parent’ => 0,
    ‘terms’ => $list_ids,
    ‘operator’ => ‘NOT IN’
    );

    $query->set( ‘tax_query’, $tax_query );
    }
    }
    }

    add_action( ‘pre_get_posts’, ‘change_parent_category_query’ );

    I will try to describe the problem, I do not want the parent category to display products from child categories, only those products that I myself will select the checkbox, example:

    Category “tables” it has a subcategory of wooden, metal and other

    In the category tables are displayed randomly goods from the categories of wooden, metal, but it should not be so, I want to assign the goods that will be displayed in the parent category “Tables”, go for example to the product Wooden table and select the display in the category Tables.

    I have thrown our category Latex, it shows a lot of products from child categories, but in fact should be only 1 product displayed, because 1 single product added to it.

    Please contact administrator
    for this information.
    Files is visible for topic creator and
    support staff only.
    Avatar: Justin
    Luca Rossi
    Support staff
    March 18, 2025 at 10:43

    Hi @Goostaf,

    Please try with the following code:

    
    // Modify product queries to exclude products from subcategories of the current parent category
    function change_parent_category_query($query_args) {
    
    	// Only affect frontend product queries
    	if (!is_admin() && $query_args['post_type'] === 'product') {
    
    		global $wpdb;
    		$parent_id = get_queried_object()->term_id;
    
    		// Ensure we have a valid parent category ID
    		if (isset($parent_id) && is_numeric($parent_id)) {
    
    			// Get all subcategory term_taxonomy_ids for the parent category
    			$all_subcategory_ids = $wpdb->get_results(
    				$wpdb->prepare(
    					"SELECT term_taxonomy_id 
                         FROM {$wpdb->prefix}term_taxonomy 
                         WHERE taxonomy = 'product_cat' 
                         AND parent = %d",
    					$parent_id
    				)
    			);
    
    			$list_ids = array();
    
    			// Collect subcategory IDs in an array
    			foreach ($all_subcategory_ids as $asi) {
    				$list_ids[] = esc_attr($asi->term_taxonomy_id);
    			}
    
    			// Get the existing tax_query if any
    			$tax_query = $query_args['tax_query'];
    
    			// Add our condition to exclude these subcategories
    			$tax_query[] = array(
    				'taxonomy' => 'product_cat',
    				'field'    => 'term_id',
    				'parent'   => 0,
    				'terms'    => $list_ids,
    				'operator' => 'NOT IN'
    			);
    
    			// Set the modified tax_query back to the query
    			$query_args['tax_query'] = $tax_query;
    		}
    	}
    }
    
    // Hook our function to pre_get_posts
    add_filter('woocommerce_products_widget_query_args', 'change_parent_category_query', 999);
    

    For any additional customizations beyond the default functionalities, we kindly invite you to submit a request through our (https://www.8theme.com/account/#etheme_customization_panel). Please be advised that customization services may incur additional charges.

    If you have any further questions or require assistance, please do not hesitate to contact us.

    Best regards,
    8Theme Team

  • Viewing 6 results - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.Log in/Sign up

Helpful Topics

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.