• Resolved cosmoweb

    (@cosmoweb)


    Hi,

    I am trying to sort out of stock products at the end of product list in search results page, but I can’t. I have add this code:

    function _nok_order_by_stock_status($posts_clauses, $query) {

    // Check if 'orderby' exists in the posts_clauses and if 'order' request parameter is set to 'desc'
    if (isset($posts_clauses['orderby']) && isset($_REQUEST['order']) && strtolower($_REQUEST['order']) == "desc") {
        $posts_clauses['orderby'] = str_replace("ASC", "DESC", $posts_clauses['orderby']);
    }
    
    // Check if the query is the main query and if it's a product category, tag, taxonomy or shop page
    if ($query->is_main_query() && (
        (function_exists('is_product_category') && is_product_category()) ||
        (function_exists('is_product_tag') && is_product_tag()) ||
        (function_exists('is_product_taxonomy') && is_product_taxonomy()) ||
        (function_exists('is_shop') && is_shop()) || is_search()
    )) {
    
    
        global $wpdb;
    
        // Join the postmeta table for stock status and price
        $posts_clauses['join'] .= "
            LEFT JOIN (
                SELECT post_id, meta_value 
                FROM $wpdb->postmeta  
                WHERE meta_key = '_stock_status'
            ) istockstatus ON ($wpdb->posts.ID = istockstatus.post_id)
            LEFT JOIN (
                SELECT post_id, meta_value 
                FROM $wpdb->postmeta  
                WHERE meta_key = '_price'
            ) prezzo ON ($wpdb->posts.ID = prezzo.post_id)";
    
        // Modify the orderby clause to prioritize out of stock products with a non-zero price
        $posts_clauses['orderby'] = "
            CASE
                WHEN istockstatus.meta_value = 'outofstock' AND prezzo.meta_value != 0 THEN 1
            END ASC, " . $posts_clauses['orderby'];
    }
    
    return $posts_clauses;
    
    }
    
    // Add the function to the 'posts_clauses' filter with a priority of 2000
    add_filter('posts_clauses', '_nok_order_by_stock_status', 2000, 2);

    It works perfectly on product categories, but in search results nothing goes right.

    What can I do?

    Please help,
    thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @cosmoweb,

    The issue seems to arise from the conditional check in your function that verifies if the current query is for a product category, tag, taxonomy, shop page, or search.

    The problem is that the is_search() function might not behave as expected in this scenario. This function returns true when WordPress is using the search.php template file to render the search results. However, WooCommerce might not use this template file for product search results, which could be why your code isn’t working.

    As a solution, you could try replacing is_search() with is_product_search(). This function is part of WooCommerce and checks if the current page is a product search page.

    Let us know how that goes. Looking forward to helping you.

    Thread Starter cosmoweb

    (@cosmoweb)

    Hi @shameemreza,

    I tried to replace with is_product_search() but debug.log say: PHP Fatal error: Uncaught Error: Call to undefined function is_product_search()

    Thanks in advance

    Hi there @cosmoweb ??

    Sort out of stock products at the end in search results page

    As my programming knowledge is limited, I went ahead and did a web search about this.

    My understanding is that the following results that surfaced will prove useful:

    I trust that points you in the right direction, but if you have more questions, let us know.

    We’re happy to help.

    • This reply was modified 11 months, 1 week ago by anastas10s. Reason: typo
    Thread Starter cosmoweb

    (@cosmoweb)

    Hi @woo-hc,

    the code works perfectly, but not for the search results, I also put ( is_search() ) but it doesn’t work, can you help me please? Thank you

    Hi there @cosmoweb ??

    Please note that we can’t provide support for code customization as per our support policy. Still, if you need customization, we do our best to offer advice and direct you to appropriate resources. Based on the feedback we get from our customers, we highly recommend contacting here.

    I hope that helps!

    • This reply was modified 11 months, 1 week ago by anastas10s. Reason: URL typo
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Sort out of stock products at the end in search results page’ is closed to new replies.