Sort products by DESC order in Woocommerce

12,681

Solution 1

add_action( 'woocommerce_product_query', 'default_catalog_ordering_desc', 10, 2 );
function default_catalog_ordering_desc( $q, $query ){
    if( $q->get( 'orderby' ) == 'date' )
        $q->set( 'order', 'DESC' );
}

Solution 2

You can use a custom function hooked in woocommerce_product_query action hook this way:

add_action( 'woocommerce_product_query', 'default_catalog_ordering_desc', 10, 2 );
function default_catalog_ordering_desc( $q, $query ){
    if( $q->get( 'orderby' ) == 'menu_order title' )
        $q->set( 'order', 'DESC' );
}

Code goes in functions.php file of your active child theme (or active theme) or in any plugin file.

Tested and works.

Solution 3

You need to decide first that by which field you want to sort either price title etc. But i have written all the possible ways u can use it,

add_filter( 'woocommerce_get_catalog_ordering_args','custom_query_sort_args' );

function custom_query_sort_args() {

    // Sort by and order
    $current_order = ( isset( $_SESSION['orderby'] ) ) ? $_SESSION['orderby'] : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

    switch ( $current_order ) {
        case 'date' :
            $orderby = 'date';
            $order = 'desc';
            $meta_key = '';
        break;
        case 'price' :
            $orderby = 'meta_value_num';
            $order = 'asc';
            $meta_key = '_price';
        break;
        case 'title' :
            $orderby = 'meta_value';
            $order = 'asc';
            $meta_key = '_woocommerce_product_short_title';
        break;
        default :
            $orderby = 'menu_order title';
            $order = 'asc';
            $meta_key = '';         
        break;
    }

    $args = array();

    $args['orderby']        = $orderby;
    $args['order']          = $order;

    if ($meta_key) :
        $args['meta_key'] = $meta_key;
    endif;

    return $args;

}

Solution 4

This code should be added to functions.php of your theme

add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');

function custom_default_catalog_orderby() {
     return 'date'; // Can also use title and price
}
Share:
12,681
sobhan
Author by

sobhan

Updated on June 04, 2022

Comments

  • sobhan
    sobhan almost 2 years

    I wrote this code in functions.php file of my active theme:

        add_filter( 'woocommerce_get_catalog_ordering_args', 
        'custom_woocommerce_get_catalog_ordering_args' );
        function custom_woocommerce_get_catalog_ordering_args( $args ) {
         $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
        if ( 'random_list' == $orderby_value ) {
            $args['orderby'] = 'date';
            $args['order'] = 'desc';
            $args['meta_key'] = '';
        }
        return $args;
    }
    add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
    function custom_woocommerce_catalog_orderby( $sortby ) {
        $sortby['random_list'] = 'DESC SORT';
        return $sortby;
    }
    

    I am trying to sort products by Desc, but products still sorting by default (ASC).

    How can I sort them by DESC?

  • sobhan
    sobhan over 6 years
    what is deferent with my code? i want sort by date desc and i already try $args['orderby'] = 'date'; $args['order'] = 'desc';
  • baiju jha
    baiju jha over 6 years
    Can u explain me what does this mean if ( 'random_list' == $orderby_value )
  • baiju jha
    baiju jha over 6 years
    First u have to decide about the field in switch case then u can proceed it further as i have shown already.
  • sobhan
    sobhan over 6 years
    i try your code too! sort by date works like before but is asc and order desc not working
  • LoicTheAztec
    LoicTheAztec over 6 years
    Kindly, but this is just a minor customization on my answer code… In your question you are just asking to change default ordering to DESC. The default order-by in woocommerce is 'menu_order title' but not 'date'… So this is really not fair. If you do it this way, nobody will answer your questions… please think about it. I hope you will react in the right way, removing this…