Search code examples
wordpresssortingwoocommerce

WooCommerce - Order products by most popular but override for some products


I have my products ordered by most sold, with the built in function in Woocommerce. But I want to put a few products on the top even if they are not the most sold. Can this be done with some custom code?

For example, create a custom sort by most sold, but if these products ID(s) are present, put them on top?


Solution

  • I manage to solve this by using this code:

    add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' );
    function custom_default_catalog_orderby() {
      if (is_product_category( array( 'cat-123' ))) {
        return 'date';
      }
    }
    

    For a specific category I change the orderby to date and then set date accordingly as the order that I want. The rest of the products in all the other categories are sorted by most sold.

    Not the way I asked the question, but its another way to achieve what I want.