How to add sort by name in sorting options of Woocommerce

In this tutorial we’ll learn how to add sort by name option in sorting options of dropdown.

This is the default sorting dropdown on shop page.

woocommerceNow add the following lines of code at the end of functions.php file.

functionmonkey_ordering_args( $sort_args ) {

 

$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

switch( $orderby_value ) {

 

   // Name your sortby key whatever you'd like; must correspond to the $sortby in the next function

case 'slug':

       $sort_args['orderby'] = 'name';

       // Sort by ASC because we're using alphabetic sorting

       $sort_args['order']   = 'asc';

break;        

}

 

return $sort_args;

}

add_filter( 'woocommerce_get_catalog_ordering_args', 'monkey_ordering_args' );

 

 

// Add these new sorting arguments to the sortby options on the frontend

functionmonkey_add_new_orderby( $sortby ) {

 

// Adjust the text as desired

$sortby['slug'] = __( 'Sort by name', 'woocommerce' );

 

return $sortby;

}

add_filter( 'woocommerce_default_catalog_orderby_options', 'monkey_add_new_orderby' );

add_filter( 'woocommerce_catalog_orderby', 'monkey_add_new_orderby' );

You can access functions.php file here:

woocommerceNow you can see the sort by name option in dropdown as well.

woocommerce

 

Waqas

I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

Leave a Comment

Your email address will not be published. Required fields are marked *