tushonline
tushonline

Reputation: 290

Woocommerce - Different 'default' sorting for different categories

I'm trying to override the default sorting option (from settings), to a price based sorting for a few categories where lowest price products should be at the top.

For this I'm trying with

 if ( is_product_category( 'shirts' ) ) {
    add_filter('woocommerce_get_catalog_ordering_args', 'tk_woocommerce_catalog_orderby');
function tk_woocommerce_catalog_orderby( $args ) {
    $args['orderby'] = 'price';
    $args['order'] = 'asc'; 
    return $args;
}
  }

But, it's not working. What's missing?

How can we achieve Price based (lowest to highest) sorting for specific categories in Woocommerce 2.1.x ?

Thanks

Upvotes: 1

Views: 1976

Answers (1)

sabarnix
sabarnix

Reputation: 775

You just need to add the condition inside the filter callback like this

add_filter('woocommerce_get_catalog_ordering_args', 'tk_woocommerce_catalog_orderby');
function tk_woocommerce_catalog_orderby( $args ) {
    if( is_product_category( 'shirts' ) ) {
        $args['orderby']  = 'meta_value_num';
        $args['order']    = 'ASC';
        $args['meta_key'] = '_price'; 
    }
    return $args;
}

Upvotes: 3

Related Questions