How to Sort WooCommerce Categories for Better User Experience

Sort WooCommerce Product CategoriesIf you want to change the default order of the category in your WooCommerce site, there are several ways to sort WooCommerce product categories as outlined and illustrated on this quick guide. If you are looking for a way to sort categories, this post will guide you.

Guide to Sort WooCommerce Product Categories

You will be surprised how easy it is to  change categories order using inbuilt WooCommerce effective ways. By default, WooCommerce categories are displayed as shown on the image below:

default categories

Why Sort WooCommerce Categories ?

Using categories for your products often influences how readers approach your content. Visitors on your WooCommerce store should be able to easily find products through the product categories.

WooCommerce allows you to display either product, categories on the shop page, or subcategories on the category pages on your archive pages.

Normally, many WooCommerce users select the third option, products, and categories, or subcategories. If you do this, it means that visitors can either select products right from the home page or refine their search by clicking through to a product category archive.

Additionally, product categories help you to keep your store organized if you have a variety of products. For example, if you are selling belts and t-shirts, they should not be in the same category.sort woocommerce product categories

This implies that your store will always be organized and your customers will easily find products that fall into a certain group in your store. These taxonomies help you to have a well-organized product catalog that customers will enjoy using to find products, proving to be a very valuable asset in your store.

It is however important to note that WordPress arranges this taxonomy in alphabetical order, by default, which makes the category list look inflexible and meaningless sometimes. This necessitates the need of sorting the category differently and more interestingly in WordPress. This is the same concern that WooCommerce store owners have.

In this post, you will learn how to sort WooCommerce categories in the order that you like. This can be of great help, and you can order product categories in descending order of their IDs to help customers see the latest categories of products that are added by you whenever they visit your store.

1.    How to sort WooCommerce product categories using a shortcode

If you are familiar with WooCommerce, you know that it provides a lot of shortcodes to be used for displaying products and product categories. For instance, the shortcode [product_categories] can be used to display categories on any page.

Additionally, for this simple tutorial, we are going to use two parameters. The first one is the ‘orderby’ parameter that the following accepted values: ID, title, and name. The second parameter is ‘order’ that accepts these values: ASC and DESC.

a)    Sorting the WooCommerce product categories using orderby parameter

This parameter can be assigned different values depending on what we wish to order the categories by. By default, they are ordered by their names, so we will assign this parameter the ‘ID’ value. This is the shortcode that we will be using [product_categories orderby=’ID’]

Steps to sort the WooCommerce product categories using orderby parameter

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Pages, then select the page that you want to add the product categories. For this example, we are going to use the Home page as shown below:adding the orderby parameter shortcode
  3. To check the outcome, you need to go to the page where you have added the shortcode. Since we added it on the home page, the outcome will be:sorting categories using Orderby parameter

The categories will now be displayed using the ID.

b)    Sorting the WooCommerce product categories using order parameter

Similar to the orderby parameter, this parameter can be assigned different values depending on what we wish to order the categories by. By default, they are ordered in ascending order(ASC), so we will assign this parameter the ‘DESC’ value. This is the shortcode that we will be using [product_categories orderby=’ID’ order=’DESC’].

Steps to sort the WooCommerce product categories using order parameter

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Pages, then select the page that you want to add the product categories. For this example, we are going to use the Home page as shown below:adding the order parameter shortcode
  3. To check the outcome, you need to go to the page where you have added the shortcode. Since we added it on the home page, the outcome will be:sorting categories using Order parameter

The categories will now be displayed in descending order.

2.    Sorting WooCommerce product categories using a code snippet

This is a developer-friendly solution that can help you archive the same results as using the shortcodes as indicated in the above examples. Similarly, we are going to use two parameters. The first one is the ‘orderby’ parameter that the following accepted values: ID, title, and name. The second parameter is ‘order’ that accepts these values: ASC and DESC.

a)    Sorting the WooCommerce product categories using code

This is a very effective way to sort your WooCommerce product categories. Just follow these simple steps:

Steps to sort the WooCommerce product categories using code

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will sort your WooCommerce product categories.
  3. Add the following code to the php file and remember to save the changes once you are done:
add_filter( 'woocommerce_product_subcategories_args', 'njengah_woocommerce_get_subcategories_ordering_args' );

function njengah_woocommerce_get_subcategories_ordering_args( $args ) {

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

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

return $args;

}
  1. To check the outcome, you need to go to the home page. You should see this:sorting categories using Order parameter

b)    Sorting the WooCommerce product categories using another code

Steps to sort the WooCommerce product categories using code

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will sort your WooCommerce product categories.
  3. Add the following code to the php file and remember to save the changes once you are done:
add_filter( 'woocommerce_product_subcategories_args', 'njengah_woocommerce_get_subcategories_ordering_args' );

function njengah_woocommerce_get_subcategories_ordering_args( $args ) {

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

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

return $args;

}
  1. To check the outcome, you need to go to the home page. You should see this:

How the code works

The first code is used to display the categories in the descending order of their titles (names) on the Shop page using $args['order'] = 'desc'; and $args['orderby'] = 'title';. For the second code, it id used to sort these categories in the descending order of their IDs. The value that is assigned to the orderby parameter will change from ‘name’ to ‘ID’ in the code snippet.

Conclusion

We have seen that it is very easy to implement your solution when it comes to sorting categories. In this post, we have looked at how you can sort WooCommerce categories. We can achieve these by using the order and orderby parameters and assigning them different values to achieve the sorting effect that you need.

The values name and title are used to order the categories in the order of their names. I hope that you learned how to sort WooCommerce categories from this post or at least you have an ideal on how to get started.

Similar Articles

Comments are closed.