问题
I'm building a store for books. I have a bunch of grouped products. My pain is, that woocommerce lists both type by default. I only need the parent of the group to be listed without childrens.
Is there any hook or workaround for this?
Thanks.
回答1:
Try the following:
add_action( 'woocommerce_product_query', 'so_27975262_product_query' );
function so_27975262_product_query( $q ){
$q->set( 'post_parent', 0 );
}
The idea is that we're modifying the query such that it will only show top-level items.... thus (in theory) nothing that has been assigned to a group, which would then have the group product's ID as the post_parent
.
回答2:
The above answers do not work when you perform filters
This works with filters and does not overwrite other queries:
add_action( 'woocommerce_product_query', 'only_grouped_products_query' );
function only_grouped_products_query( $q ) {
//get current loop query
$taxonomy_query = $q->get('tax_query') ;
//appends the grouped products condition
$taxonomy_query['relation'] = 'AND';
$taxonomy_query[] = array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'grouped'
);
$q->set( 'tax_query', $taxonomy_query );
}
回答3:
Just to add to this, there is a way to do this without having to code anything.
You can simply choose to hide the product using the visibility option.
The quickest way to do this is to go to Products > Products and tick all the products you want to hide. Click the Bulk Actions drop-down and choose Hidden in the Visibility drop-down.
Some CSV import plugins should have this option too. I use Woocommerce Import CSV plugin and set the heading 'visibility' to hidden for the products I don't want to show in the catalog or search.
I have to hide all but the parent products for a price comparison site.
Hope this helps anyone looking for similar solution.
回答4:
Try the following code:
add_action( 'woocommerce_product_query', 'hs_product_query' );
function hs_product_query( $q ){
$product_type = array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'grouped'
);
$q->set('tax_query', array($product_type) );
$q->set( 'post_parent', 0 );
}
It worked for me..
来源:https://stackoverflow.com/questions/27975262/woocommerce-only-show-grouped-products-parent-in-the-loop