简体   繁体   中英

WooCommerce exclude products from child category in parent category

What I try to accomplish in WooCommerce: I have many categories (eg shoes) with child categories (eg heels, mules, wedges). All standard shoes are only in the parent category and a few special shoes (like heels, mules and wedges) are only in child categories.

If a user opens the category "shoes", I want to display only the products in the current category (not these from the child categories).

What I've tried so far I searches a lot and tried many different approaches, but no chance.

Approach #1: as seen here

function exclude_product_cat_children( $wp_query ) {
    if ( isset( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query() ) {
        $wp_query->set( 
            'tax_query', array( array (
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $wp_query->query_vars['product_cat'],
                'include_children' => false
            ) )
        );
    }
}  
add_filter('pre_get_posts', 'exclude_product_cat_children', 99);

Approach #2: as seen here

add_filter( 'parse_tax_query', 'cyb_do_not_include_children_in_company_category_archive' );
function cyb_do_not_include_children_in_company_category_archive( $query ) {
    if ( 
        ! is_admin() 
        && $query->is_main_query()
        && $query->is_tax( 'shoes' )
    ) {
        $query->tax_query->queries[0]['include_children'] = 0;
    }
}

I really don't know how to fix this issue. It seems like this two approaches are working for all - just not for me. Any ideas?

Thank you!

I finally found the solution myself. Hopefully I can help someone with the code below.

What is the code for? I hide all products in child categories in the current (parent) category.

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' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM