testingexpert
testingexpert

Reputation: 53

Woocommerce Default sorting not working when set custom ordering

I am using wordpress+woocommerce and getting issue in sorting. By default i have set option "Custom Ordering+Name" in woocommerce settings. But my other filters like sort by price, Popularity are not working. I have tried to use this hook and it's working but it shows by default all products with highest to low price. I want to set "Custom ordering" when page will load first time and then if user will choose other option like price, popularity then ordering should work according to selected filters.

add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
    $args['meta_key'] = '_price';
    $args['orderby'] = 'meta_value_num';
    $args['order'] = 'desc'; 
    return $args;
}

Upvotes: 2

Views: 10074

Answers (1)

Andrew Schultz
Andrew Schultz

Reputation: 4243

You can you use this code in your functions.php file to set the combobox filter to Sort by price: high to low. I don't know why your filters are not working, they work fine for me in WooCommerce 3.3.3.

add_filter('woocommerce_default_catalog_orderby', 'modify_woocommerce_default_catalog_orderby');

function modify_woocommerce_default_catalog_orderby( $orderby ) {
    if( empty( $orderby ) ) {
        return 'price-desc';
    }

    return $orderby;
}

Upvotes: 3

Related Questions