简体   繁体   中英

Woocommerce Excluding Certain Category from Shop Page

Trying to exclude a single category from my WooCommerce's Shop Page

I was using this code but it would break my sites's attributes filter links:

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;

    if ( ! is_admin() && is_shop() ) {

        $q->set( 'tax_query', array(array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'samples' ), 
           'operator' => 'NOT IN'
        )));

     }

     remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

This solution excluded the category from Shop Page. But the problem is, the Category archive page for the excluded category says no products available.

I've E-Books category which I don't want to enlisted on Shop Page. I have created a separate menu item for E-Books, which is where I want to list all books from the E-Books category.

How to achieve this?

Update

I added if (!$q->is_main_query() || !is_shop()) return; to the action hook and it resolved my above mentioned problem. This line excludes the category only from Shop page, however all products are listed good when the excluded category is accessed directly from menu (category page).

function custom_pre_get_posts_query( $q ) {

if (!$q->is_main_query() || !is_shop()) return;

$tax_query = (array) $q->get( 'tax_query' );

$tax_query[] = array(
       'taxonomy' => 'product_cat',
       'field' => 'slug',
       'terms' => array( 'ebooks' ),
       'operator' => 'NOT IN'
);


$q->set( 'tax_query', $tax_query );

}

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

You could use the woocommerce_product_query hook, which is pretty similar to pre_get_posts except it already has the appropriate conditional logic. There is also a woocommerce_product_query_tax_query filter, but I'm not sure if that is exists in WooCommerce 2.6 or if its new in 2.7 beta.

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    $q->set( 'tax_query', array(array(
       'taxonomy' => 'product_cat',
       'field' => 'slug',
       'terms' => array( 'samples' ), 
       'operator' => 'NOT IN'
    )));

}

EDIT filtering is done through the taxonomy query, and in the above example we're overriding the tax query completely. I can't test that this works right now (and arrays of arrays are tricky so I may have messed up), but the theory is that we need to merge the new constraint with the existing taxonomy query generated by WooCommerce.

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'samples' ), 
           'operator' => 'NOT IN'
    );


    $q->set( 'tax_query', $tax_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