How to Add Sort By Stock In WooCommerce

WooCommerce Sort By StockIf 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.

productsYou 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.

stock managementThis is how stock availability is displayed on the product page:

stockLet 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:

  1. Log in to the WordPress Dashboard as admin.
  2. From the dashboard, navigate to Appearance > Theme Editor. Open the functions file to add the function to sort products by stock.
  3. 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 );

 

  1. This is the outcome:availability

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.

Similar Articles