Sorting WooCommerce Products, out of stock put at the end of the list of goods

I recently received an audit from a client and there was one interesting task in it that is often implemented in online stores for the convenience of visitors “WooCommerce product sorting, out of stock put at the end of the list of goods”.

The first thing I did was looking for how to solve the problem using WooCommerce, but never found. By default, the store plugin offered to exclude products from the list in the category and if there were no more products in stock in the category, it would disappear from the menu, while links to products would remain active. The idea is certainly good., and search is ok, but the fact that the category will disappear or remain empty is not good. And then I started google-it.

After some digging, I found 2 solutions which I will discuss below.. Which one to use is up to you!

Sorting WooCommerce Products with add_action( ‘pre_get_posts’, … )

With the help of an action and a function, we receive a request that will be sent to the database, if the request is received successfully, then modify it with . $query->set( ‘meta_key’, ‘_stock_status’ ) and $query->set( ‘meta_key’, ‘_stock_status’ ). These 2 simple lines mean in human language the following. Sort received data by field _stock_status in alphabetical order if 2 goods will be the same date, put the newer item first. The code for this action needs to be added to the functions.php file

The code is simple and working. This action can also be done using the plugin WooCommerce Extra Product Sorting Options.

Sorting products by availability using the WooCommerce Extra Product Sorting Options plugin

There is a great plugin for sorting products WooCommerce Extra Product Sorting Options in which you can choose sorting methods through the WordPress admin panel, and then in the WooCommerce settings, select the default sorting by the presence of goods. You can do this through the menu in the admin panel. WooCommerce -> Settings -> Products tab -> Display -> Item Default Product Sorting and Add Product Sorting. Also, using the plugin, you can add several new sorting options to the filter.

The plugin works in English and you will have to use the program to translate it Poedit or plugin Loco Translate. Before starting the translation, do not forget to make copies of the site files. Successful work on convenient online stores, that search engines love.


Купить хостинг WordPress
/* WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal