简体   繁体   中英

Show product variations on WooCommerce archive pages as shop

After a long search and a lot of trial and error I found the code below to display variable products in the shop and on the category page. After a small modification it works fine. But it also displays the variations in all categories, instead of only showing the variations of the current category.

add_action( 'pre_get_posts', 'custom_modify_query_get_posts_by_date' );

// Modify the current query

function custom_modify_query_get_posts_by_date( $query ) {
 if ( ! is_admin() && $query->is_main_query() ) {

    if ( is_post_type_archive( 'product' ) || is_product_category() || is_product_tag() ) {
       $query->set( 'order', 'ASC' );
           add_filter( 'posts_where', 'rc_filter_where' );
   }
     return $query;
 }
}

// Add products variation post type to the loop
function rc_filter_where( $where = '' ) {

   $type = 'product_variation';
   $where .= " OR post_type = '$type'";

   return $where;

}

I figured if I can limit the pre_get_posts to the current category that should work. Tried a lot of things, but I can't get it to work.

add_action( 'pre_get_posts', 'custom_modify_query_get_posts_by_date' );

// Modify the current query

function custom_modify_query_get_posts_by_date( $query ) {
 if ( ! is_admin() && $query->is_main_query() ) {

 if ( is_product_category() ) {
    $cate = get_queried_object();
    $cateID = $cate->term_id;

       $query->set( 'order', 'ASC' );
       $query->set('cat', $cateID);
           add_filter( 'posts_where', 'rc_filter_where' );
   } elseif ( is_post_type_archive( 'product' ) || is_product_tag() ) {
       $query->set( 'order', 'ASC' );
           add_filter( 'posts_where', 'rc_filter_where' );
   }
     return $query;
 }
}

// Add products variation post type to the loop
function rc_filter_where( $where = '' ) {

   $type = 'product_variation';
   $where .= " OR post_type = '$type'";

   return $where;
}

So I would like to know if it's possible to show all product variations in the shop and on the category view only show the variations that belong to that category.

Could you remove the posts_where's and try tax_query instead of $query->set('cat', $cateID);?

add_action( 'pre_get_posts', 'custom_modify_query_get_posts_by_date' );

// Modify the current query

function custom_modify_query_get_posts_by_date( $query ) {
if ( ! is_admin() && $query->is_main_query()  && $query->is_tax('product_cat') ) {

if ( is_tax('product_cat')  ) {
$cate = get_queried_object();
$cateID = $cate->term_id;
$cateslug = $cate->slug;
$catetax = $cate->taxonomy;

//$query->set('cat', $cateID); 

$taxquery = array(
    'tax_query' => array(
    'taxonomy' => $catetax,
    'terms' => $cateslug,
    'field' => 'slug',
    'include_children' => true,
    'operator' => 'IN'
      )
    );

$query->set( 'tax_query', $taxquery );

$query->set( 'post_type', array('product', 'product_variation') );

   $query->set( 'order', 'ASC' );

} elseif ( is_post_type_archive( 'product' ) || is_product_tag() ) {
   $query->set( 'order', 'ASC' );

}
 return $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