• Resolved doffine

    (@doffine)


    Hey there WooCommerce support & enthusiasts,

    to change the default sort order of only a single product category we just used the good working code snippet…

    //custom function to override default sort by category
    function custom_default_catalog_orderby() {
    //choose categories where default sorting will be changed
    if (is_product_category( array( 'category1', 'category2', 'category3' ))) {
        return 'date'; // sort by latest
    }else{
    return 'popularity'; // sort by popularity as the default
      } // end if statement
    } //end function
    
    add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' ); //add the filter

    found at https://webhostingbuddy.com/blog/woocommerce-default-sort-method-for-specific-category/

    Now this code works as charm, but it sets all “other” categories per default to a static value like in this case “popularity”.

    This overrides the setting made in WooCommerce.

    For this code to be not only super but ingenious we would like that not a static order like “popularity” is used when the initial categories don’t apply, but simply the order setting made in WooCommerce itself. That would be fantastic!

    So does anyone have an idea how we could modify the code passage…

    }else{
    return 'popularity'; // sort by popularity as the default
    }

    … to something like “else return ‘order setting made in WooCommerce'”?

    With many thanks in advance,
    -doffine

Viewing 2 replies - 1 through 2 (of 2 total)
  • The default value is sent to your function as a parameter, so return that.

    function custom_default_catalog_orderby( $default_orderby ) {
    ...
    return $default_orderby;
    Thread Starter doffine

    (@doffine)

    @lorro, you are our personal hero! This works perfectly, we already tested it. Great!

    I mark this as resolved now.

    Many thanks again for this help and idea,
    -doffine

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom category sort order’ is closed to new replies.