Move Out Of Stock Products To End Of Archive Loop

8

Move Out Of Stock Products To End Of Archive Loop woocamerc

Having non-existent products in the store's product list will tire customers and reduce your sales, so the best solution is to move the non-existent products to the bottom of the list.

view Other methods of increase Online store income

To move non-existent products to the end of the list, just put the following code at the end of the template function file.


add_filter('posts_clauses', 'order_by_stock_status');
function order_by_stock_status($posts_clauses) {
    global $wpdb;
    // only change query on WooCommerce loops
    if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {
        $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;
}

Leave a Reply

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

eighteen − 5 =

 
Online-Chat

We are ready to answer your questions