WooCommerce has a small army of filters and hooks that you could customize almost every aspect of the plugin. If you have a grouped product the default display method is by menu_order
. But recently, someone wanted to display the grouped items by the date they were published.
This is very easily accomplished by filtering the $args
being passed through the woocommerce_grouped_children_args
filter. WooCommerce queries for the child products in the grouped product via WP_Query
so you can essentially use any parameter supported by WP_Query
. In this case, we only need to change the orderby
parameter to date
and since we’d like the most recent items first, we swap the order
parameter to descending.
add_filter( 'woocommerce_grouped_children_args', 'kia_grouped_children_args' );
function kia_grouped_children_args( $args ){
$args['orderby'] = 'date';
$args['order'] = 'DESC';
return $args;
}
Code language: PHP (php)
If you need help understanding filters, I wrote what I think is a pretty good tutorial on how to use filters. It took me a while to understand them, but once you do they are very powerful and let you make a lot of customizations.
If you aren’t seeing any changes, chances are that the grouped product’s children have already been stored in a transient. You will need to delete this transient to see changes right away. You can clear all WooCommerce product transients via the Admin. Navigate to WooCommerce>System Status>Tools and click on the button to clear transients.
Comments
3 responses to “Change the sort order of WooCommerce Grouped Products”
Thanks Kathy. Very usefull. I need this kind of filter but I’ve used the pre_get_post filter on CPT product and it works fine too.
Cheers
Grégoire
Yup!
woocommerce_product_query
is simply run when WooCommerce filterspre_get_posts
so both ways will work just fine for products. à+Hi Kathy, I’ve been trying to contact you via your contact page but it doesn’t appear to be working?