• Resolved cecowe

    (@cecowe)


    Hello, Is there any way to sort search results by product “Position” automatically in the search box result and in the search results page via snippet or functions.php?

    Thanks ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author ILLID

    (@mihail-barinov)

    Hello,

    This “Position” is some attribute or taxonomy?
    Anyway you can do this by using some code snippets. Please read this article to find examples of code snippets for sorting search results.

    Regards

    Thread Starter cecowe

    (@cecowe)

    Actually I meant Menu Order while talking about Position
    https://prnt.sc/v0k71r
    Where the lower is the number, the higher is the priority to show in upper positions.

    Plugin Author ILLID

    (@mihail-barinov)

    Thanks, now I understand. In this case please use code

    add_filter( 'aws_search_results_products', 'my_aws_search_results_products' );
    function my_aws_search_results_products( $products ) {
        usort($products, function ($item1, $item2) {
            $product1 = wc_get_product( $item1['id'] );
            $product2 = wc_get_product( $item2['id'] );
            $a = $product1->get_menu_order();
            $b = $product2->get_menu_order();
            if ($a == $b) {
                return 0;
            }
            return ($a < $b) ? 1 : -1;
        });
        return $products;
    }
    Thread Starter cecowe

    (@cecowe)

    Hello Misha, Thank you for responding but it doesn’t work.

    I’ve cleared plugin cache but it’s not working. The products I’m search for are not appearing first.

    Plugin Author ILLID

    (@mihail-barinov)

    You don’t have these products inside the search results box at all or they are displayed somewhere at the bottom?

    Thread Starter cecowe

    (@cecowe)

    They are displayed at the bottom.

    Plugin Author ILLID

    (@mihail-barinov)

    Please check one more time that this products has menu order. Code that I give you show product with higher menu order first.

    Thread Starter cecowe

    (@cecowe)

    Hey Misha,

    By default woocommerce sets priority while using LOWER menu order. https://www.endocreative.com/wp-content/uploads/2015/06/Edit_Product_%E2%80%B9_Authentic_Manhood_%E2%80%94_WordPress-1024×477.png

    So my most important items are with lower number for menu order.

    If the code shows lowest menu order first, it will work correctly.

    Thread Starter cecowe

    (@cecowe)

    Replacing < with > made it work. Thank you so much.

    Is there any way to do the same for Search Bar Results?

    Plugin Author ILLID

    (@mihail-barinov)

    You mean – for ajax search results block? If so – than this code snippet must work for it to.

    Thread Starter cecowe

    (@cecowe)

    Oh my fault. Thank you Mihail! ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Sort search results by product “Position” automatically’ is closed to new replies.