skip to Main Content

I’m looking for a way to sort the products by star rating (asc and desc). It seems that i would need to create a custom code for this since something like this for example isn’t implemented in Woocommerce.

$options['rating-asc'] is a the piece of code that doesn’t work/exist but i use it to express the function i’m looking for like $options['title-desc'].

add_filter( 'woocommerce_catalog_orderby', 'rory_add_custom_sorting_options' );

function rory_add_custom_sorting_options( $options ){
    $options['rating-asc'] = 'Rating (Asc)';
    return $options;
 
}

2

Answers


  1. Your code should work only for adding the sorting option to the dropdown, but if you want it to have effect you need to link it to a meta key or something by adding the correct arguments to the catalog products query like this:

    add_filter( 'woocommerce_get_catalog_ordering_args', 'rory_custom_sorting_args' );
     
    function rory_custom_sorting_args( $args ) {
     
        if( isset( $_GET['orderby'] ) && 'rating-asc' === $_GET['orderby'] ) {
            $args['meta_key'] = 'rating-asc'; // Replace this with the meta key you want to use for ordering products
            $args['orderby'] = array( 'meta_value' => 'ASC' );
        }
     
        return $args;
     }
    
    Login or Signup to reply.
  2. First you need to define 'rating-asc' sorting options arguments in a custom function hooked in woocommerce_get_catalog_ordering_args hook.

    As sorting option "Sort by average rating" exists, if you look to default existing arguments for sorting products by "rating" key, you have that array:

    $args = array(
        'orderby' => array(
            'meta_value_num' => 'DESC',
             'ID' => 'ASC'
        ),
        'order' => 'ASC',
        'meta_key' => '_wc_average_rating'
    );
    

    So you just need to change 'meta_value_num' => 'DESC' to 'meta_value_num' => 'ASC', then your right working code is going to be like:

    add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_ratings' );
    function enable_catalog_ordering_by_ratings( $args ) {
        if ( isset( $_GET['orderby'] ) && 'rating-asc' === $_GET['orderby']
        && isset($args['orderby']['meta_value_num']) ) {
           $args['orderby']['meta_value_num'] = 'ASC';
        }
        return $args;
    }
    

    Now you can insert your new sorting option just after "Sort by average rating" existing one like:

    add_filter( 'woocommerce_catalog_orderby', 'catalog_orderby_ratings_asc_filter' );
    function catalog_orderby_ratings_asc_filter( $options ){
        $sorted_options =[];
    
        foreach( $options as $key => $label ){
            if( 'rating' === $key ) {
                $sorted_options['rating'] = $options['rating'] . ' ' . __('(Desc)', 'woocommerce');
                $sorted_options['rating-asc'] = $options['rating'] . ' ' . __('(Asc)', 'woocommerce');
            } else {
                $sorted_options[$key] = $label;
            }
        }
    
        return $sorted_options;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    enter image description here

    Related:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search