简体   繁体   中英

Change default sorting for specific Woocommerce product category archive pages

I need to change the default product sorting option to "Newness" for a specific product category on my site. I know you can go to WooCommerce > Settings > Product > Display to globally change the default sorting option, but thats not what I need to do. I need something like:

function change_default_sorting_option(){

if(is_product_category('3555')){

//change default sorting option to newness

}

}
add_action('', 'change_default_sorting_option');

I haven't done much with the sorting functionalities so I'm not exactly sure where to start. I know the function should go in my child theme's functions.php.

here is the correct hook and the way to get for a specific product category archive page, the default sorting by "Newness":

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 20, 1 );
function custom_catalog_ordering_args( $args ) {
    $product_category = 't-shirts'; // <== HERE define your product category

    // Only for defined product category archive page
    if( ! is_product_category($product_category) ) return $args;

    // Set default ordering to 'date ID', so "Newness"
    $args['orderby'] = 'date ID';

    if( $args['orderby'] == 'date ID' )
        $args['order'] = 'DESC'; // Set order by DESC

    return $args;
}

Code goes in function.php file of your active child theme (or active theme) . Tested and works.

Here better and WORKING solution

add_filter('woocommerce_get_catalog_ordering_args', 'woocommerce_catalog_orderby');
function woocommerce_catalog_orderby( $args ) {
    if( is_product_category( 'shirts' ) ) {  // <- define category slug- returns true or false
        $args['orderby']  = 'meta_value_num';
        $args['order']    = 'ASC'; // <- order ASC or DESC
        $args['meta_key'] = '_price'; // <- _price is meta_value_num's key - required
    }
    return $args;
}

Please also read about WP_Query order & orderby here: https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

Assuming you're working within a custom page template, this should be relatively easy. Not entirely sure if your approach will work with utilizing is_page() to force the sorting of the product to change, however... You can utilize the global WP_Query to create a custom query, sorting your products however you'd like. Here's an example:

<?php  
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => -1, //or... set your post per page and utilize pagination if required.
        'orderby'        => 'title',
        'order'          => 'ASC',
    );

    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();
        global $product;

        //this is where you can output your products
        //which will be ordered according to the set arguments above.

    endwhile;

    wp_reset_query();
?>

EDIT :

If you're looking for this kind of functionality for a specific category, I would simply create a new page in your system and use the WooCommerce product short-code. You don't need to add anything in your functions.php or hook onto anything this way. The following short-code is an example you can use to...for example order your products on a specific category page by title.

[products category="yourcategory" orderby="title" ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM