• Resolved glazergallery

    (@glazergallery)


    We want to change the default sort order on only a few selected WooCommerce categories from alphabetical by title, ascending (A to Z), to alphabetical by title, descending (Z to A). For the rest of the site, however, we want the order to remain ascending.

    The type of page I want to sort is here: https://www.georgeglazer.com/wpmain/product-category/press-clippings-exhibitions/ As you can see, the titles begin with a year. We want to show the most recent events first, i.e. a title beginning “2020” would be first, followed by titles beginning “2019,” “2018,” etc. In the near future, this page will be broken into subcategories, so it would be good to know how to assign two or more categories to whatever code will accomplish this.

    I already have the following custom code in my Child Theme to make default sorting of products in all categories alphabetical, ascending order. I assume something gets added to this code, which works fine:

    * * * * *

    // DEFAULT PRODUCT SORTING ALPHABETICALLY //
    add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘custom_woocommerce_get_catalog_ordering_args’ );

    function custom_woocommerce_get_catalog_ordering_args( $args ) {
    $orderby_value = isset( $_GET[‘orderby’] ) ? woocommerce_clean( $_GET[‘orderby’] ) : apply_filters( ‘woocommerce_default_catalog_orderby’, get_option( ‘woocommerce_default_catalog_orderby’ ) );

    if ( ‘alphabetical’ == $orderby_value || ‘menu_order’ == $orderby_value ) {
    $args[‘orderby’] = ‘title’;
    $args[‘order’] = ‘ASC’;
    }

    return $args;
    }

    add_filter( ‘woocommerce_default_catalog_orderby_options’, ‘custom_woocommerce_catalog_orderby’ );
    add_filter( ‘woocommerce_catalog_orderby’, ‘custom_woocommerce_catalog_orderby’ );

    function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby[‘alphabetical’] = __( ‘Sort by A to Z’ );
    return $sortby;
    }

    * * * * *

    In doing the research on this subject I found this article about ascending vs. descending vs. random order:
    https://docs.woocommerce.com/document/custom-sorting-options-ascdesc/

    /**
    * Add custom sorting options (asc/desc) */

    add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘custom_woocommerce_get_catalog_ordering_args’ );
    function custom_woocommerce_get_catalog_ordering_args( $args ) {
    $orderby_value = isset( $_GET[‘orderby’] ) ? wc_clean( $_GET[‘orderby’] ) : apply_filters( ‘woocommerce_default_catalog_orderby’, get_option( ‘woocommerce_default_catalog_orderby’ ) );
    if ( ‘random_list’ == $orderby_value ) {
    $args[‘orderby’] = ‘rand’;
    $args[‘order’] = ”;
    $args[‘meta_key’] = ”;
    }
    return $args;
    }
    add_filter( ‘woocommerce_default_catalog_orderby_options’, ‘custom_woocommerce_catalog_orderby’ );
    add_filter( ‘woocommerce_catalog_orderby’, ‘custom_woocommerce_catalog_orderby’ );
    function custom_woocommerce_catalog_orderby( $sortby ) { $sortby[‘random_list’] = ‘Random’;
    return $sortby;
    }

    * * * * *

    And I found this article about changing the default sorting for a specific category, but it’s not exactly what I want to do because it doesn’t address the order, just the type of sorting (by date, by popularity, etc.). Also note that sorting by date means the date that the product was added to the site, not the date in the title:

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

    //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

    * * * * *

    I should mention that I don’t really understand PHP, although I can use common sense to see what the patterns are. In other words, give me a recipe and I can probably follow it.

    –Helen

    • This topic was modified 3 years, 10 months ago by glazergallery.
    • This topic was modified 3 years, 10 months ago by glazergallery.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @glazergallery ,

    If I understand correctly, this is the actual end goal – “We want to change the default sort order on only a few selected WooCommerce categories from alphabetical by title, ascending (A to Z), to alphabetical by title, descending (Z to A). For the rest of the site, however, we want the order to remain ascending.”.

    I skipped the codes you have shared as that can bring confusion.

    Here is an example code that I had to override a default order for a certain category –

    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 20, 1 );
    
    function custom_catalog_ordering_args( $args ) {
    
        $product_categories = array('tshirts', 'accessories'); // <== HERE define your product category
    
        // Only for defined product category archive page
        if( ! is_product_category($product_categories) ) return $args;
    
        // Set default ordering to title
        $args['orderby'] = 'title';
    
        if( $args['orderby'] == 'title' )
            $args['order'] = 'DESC'; // Set order by DESC
    
        return $args;
    }

    You can use the above example on your site. You are supposed to add the code in your theme’s functions.php file.

    Thank you ??

    Thread Starter glazergallery

    (@glazergallery)

    Thank you, Rashed! I am eager to try this. One question: where the code says “HERE define your product category” would I put the slug? That is, “press-clippings-exhibitions”?

    Thanks,
    Helen

    Plugin Support RK a11n

    (@riaanknoetze)

    would I put the slug

    Yes – that’ll be the slug of the category and not the title ??

    Thread Starter glazergallery

    (@glazergallery)

    Rashed and Riann,

    It worked! Thank you so much for your help!

    –Helen

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change sort order from ascending to descending on only two or three categories’ is closed to new replies.