If you are wondering how customers in your store can sort products by stock availability in WooCommerce, you are in the right place.
In today’s brief tutorial, we will illustrate how you add a new sorting option on the shop page.
It is important to note that WooCommerce does not allow customers to sort products based on stock availability.
This is why we created this post to help you out.
Read on, as we will share a custom code snippet we created to sort products by stock. Therefore, it is important to have some basic coding skills for you to implement this solution.
WooCommerce Sort By Stock
Stock management can be very challenging especially if you have stock issues due to delays or customized items.
If this is the case, it is important to set the right expectations with customers regarding their orders and stock availability.
Efficient stock control balances the need for surplus supplies with the need to reduce tied-up capital.
WooCommerce comes with a stock management feature that allows you to enable the Manage Stock option.
You can do this by heading over to WooCommerce > Settings > Products > Inventory from the admin panel.
You can also enable notifications and set thresholds for low-stock and out-of-stock products.
After that, you need to enable the manage stock option for all the products in your store. If you enable this setting, the stock for the products will be tracked. In addition, inventory will be tracked at the product level, not the variation level.
This is how stock availability is displayed on the product page:
Let us illustrate how you can sort products by stock.
Steps to Sort Products By Stock in WooCommerce
It is important to note that you can modify the template files but this is not advisable.
This is because template updates will break the changes you have made. It is always a good practice to modify templates using filters.
WooCommerce does not offer a template override system. This means that we will add our code to the functions.php file to override the templates.
Here are the simple steps you need to follow:
- Log in to the WordPress Dashboard as admin.
- From the dashboard, navigate to Appearance > Theme Editor. Open the functions file to add the function to sort products by stock.
- Add the following code in the functions.php file:
// Ordering products based on the selected values function filter_woocommerce_get_catalog_ordering_args( $args, $orderby, $order ) { switch( $orderby ) { case 'availability': $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; $args['meta_key'] = '_stock'; break; } return $args; } add_filter( 'woocommerce_get_catalog_ordering_args', 'filter_woocommerce_get_catalog_ordering_args', 10, 3 ); // Orderby setting function filter_orderby( $orderby ) { $orderby['availability'] = __( 'Availability', 'woocommerce' ); return $orderby; } add_filter( 'woocommerce_default_catalog_orderby_options', 'filter_orderby', 10, 1 ); add_filter( 'woocommerce_catalog_orderby', 'filter_orderby', 10, 1 ); // Optional: use for debug purposes (display stock quantity) function action_woocommerce_after_shop_loop_item() { global $product; echo '<div style="color: red !important; font-size: 20px !important;">' . wc_get_stock_html( $product ) . '</div>'; } add_action( 'woocommerce_after_shop_loop_item', 'action_woocommerce_after_shop_loop_item', 9, 0 );
Conclusion
By now, customers in your WooCommerce store should be able to sort products based on stock availability. However, you need to have some coding skills for you to implement this solution. You can also consult a qualified WordPress developer if you are a beginner.
We hope that this post helped you to learn more about stock management.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Change SKU Text WooCommerce
- How to Display Random Products WooCommerce
- How to Check If a Product is Variable In WooCommerce
- How to Get Product ID In WordPress
- How to Get Product Data WooCommerce
- How to Get Last Order by User Id WooCommerce
- How to Style WooCommerce Checkout Page
- 24+ Best WooCommerce Plugins for Checkout Page Customization
- How to Get Current User Role in WordPress & Display Roles
- How to Change Add to Cart Text WooCommerce Guide with Examples
- WooCommerce Get Cart After Add To Cart
- How to Add New WooCommerce Customers to Mailchimp
- WooCommerce Mobile Checkout Optimization Quick Guide
- How to Get Best-Selling Products In WooCommerce
- How to Show Product Stock on the Cart Page WooCommerce
- WooCommerce Archive Hooks [Complete Guide]
- WooCommerce Visual Hook Guide Checkout
- How to Redirect User If Not Logged in WordPress » Page Redirect
- How to Insert Default Content to WordPress Post Editor Dynamically