We’ve got a plugin that will allow you to add WooCommerce product sorting options (not to mention one that will remove core sorting options as desired), but you may need a more customized solution for sorting your products.
In this case, you may want to sort WooCommerce products by your custom fields or by other post meta values. Let’s go over how we can add sorting options for custom fields that we’ve set for our WooCommerce products. First, we’ll go over what your custom fields should look like.
Add Custom Fields to WooCommerce Products
You can quickly add custom fields to all WooCommerce products by importing them with your custom fields set as post meta. The WooCommerce Product CSV Import suite extension is great for this, and will allow you to both export and import products. This will enable you to set post meta for your products in bulk as needed — export products, add this info, and re-import them to merge your changes.
You can also add custom fields directly from the “Edit Product” screen:
This will allow you to add both text-based and number-based values for your post meta information. Once this information has been added to products, you’ll be able to use it to sort them.
Sort WooCommerce Products by Custom Field
WooCommerce has a bit of helpful information on how to sort WooCommerce products, but it doesn’t address the information we’ll need to sort on: meta values. We can sort by meta values that are both words and numbers, so we’ll do an example of each in this sample snippet.
You can add as many new sorting options as you’d like, but I’m going to add two: Sort by location (word-based) and Sort by points earned (number-based). This could be handy if you, for example, use WooCommerce Points and Rewards for a loyalty program, but some products offer double or triple points and you’d like customers to know.
Okay, so what is this doing? The first part creates the sorting arguments. It tells WooCommerce what to use to sort the products when these sorting options are used. You can add extra cases if you’d like to create more than 2 new sorting options.
Notice in the first one that I’ve sorted by
meta_value as I want to sort alphabetically, but in the second example I’ve used
meta_value_num because I want to sort numerically. I can also determine whether I should sort in ascending (
asc) or descending (
desc) order. Set these as desired.
For alphabetical sorting, I actually want to sort ascending, as “A” is a regarded as the starting value, and “Z” is regarded as an ending (higher) value. For numerical sorting, I’m going to sort descending because I want the highest points shown first.
As the last note on the first function, I then tell WooCommerce which meta key should be used. This will be the key for your custom field, which will be something like
_stock, or others (if you’re curious about the leading underscore, check this out or this).
The second function here will just add my new sorting option to the frontend of my site, as well as add it as a default option in the admin if I’d like to use it for the default. You can edit this text as desired, but I’d keep it short since it will be in your dropdown on the shop pages.
What it looks like
Okay, so ready to see what it will look like when you sort WooCommerce products in your shop? First, these options will be added to the sorting dropdown:
When I select one, the corresponding sorting arguments that we’ve added will be used. I’ve added the post meta to this screenshot so you can see it in action. Here’s my alphabetical sorting by location:
And here’s my numerical sorting by points earned for purchasing:
Pretty beautiful, huh?
A Potential Hiccup
You may notice that my examples above only include products that have these custom fields set. If these custom fields are not available, they won’t be included in the sorting options.
I like to keep it this way for stuff like “location”, as I may not want to show a bunch of products that don’t have a location assigned at all. However, for my “points” sorting, this won’t do. I want to add the rest of my products after this. You can do so by setting the
points custom field for the rest of your products, but leaving it blank.
I just did this with two products, and now they’re shown with this sorting:
This is why I recommend using the Product CSV import, as you can set all of these fields blank as desired. If you try to do this within the “Edit Product” screen, just be aware you’ll run into a minor speedbump. The product screen requires a value be set for a custom field:
To work around this, just add your fields with some arbitrary value set:
You can then delete it the value to keep the custom field blank and update your product.
Not as easy as importing, but it does the job. Now go and sort on all the meta fields you heart desires 🙂 .