I trying to sort products based on categories.
If Product category A and sub-categories sort ASC
Else if Product category B and sub-categories sort DESC.
In functions.php
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = 'countdown_date';
$args['orderby'] = 'meta_value';
// need an if statement here to switch the order
$args['order'] = 'DESC';
return $args;
}
You can use following code to solve your purpose:
Remember to change
$category_A_term_id
&$category_B_term_id
variable values to corresponding category ids.Above code will check, if current is category archive page and if it is, category
A
or child of categoryA
, change order, similarly for categoryB
.Alternatively, you can also use following WooCommerce condition:
but, here all categories must be specified.