< All Topics
Print

List Out of Stock Items on the Bottom of the Page for WooCommerce

Problem

WooCommerce lists/sorts “Out of Stock” items with “In Stock” items on the grid layout causing a hinderance for the customer to easily sort through “In Stock” products.

Solution

Navigate to the admin dashboard.

On the left panel hover over “Themes,” a popup will appear, select “Customize”

On the following page select “functions.php” on the “Theme File” right side panel.

Add the code snippet to the child theme’s functions.php file:

// SORT OUT OF STOCK ITEMS TO BOTTOM OF THE PAGE

if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
    add_filter('posts_clauses', 'order_by_stock_status', 2000);
}

function order_by_stock_status($posts_clauses) {
    global $wpdb;
  
    if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag())) {
	$posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
	$posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
	$posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
    }
	return $posts_clauses;
}

Once the code snipped has been added be sure to finalize the changes by clicking “Update File” located on the bottom of the code editor.

Now your “Out of Stock” items will be sorted or listed on the bottom of the page with “In Stock” items on top.

Troubleshooting

If you don’t see the changes appear, be sure to “Purge All” caches located on the top of the admin panel.

Source: https://wordpress.org/support/topic/show-out-of-stock-items-at-the-end-bottom/

Leave a Reply

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

Table of Contents