Change default sorting for specific Woocommerce product category archive pages

前端 未结 3 915
孤城傲影
孤城傲影 2020-12-22 02:12

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

3条回答
  •  情话喂你
    2020-12-22 03:04

    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.

提交回复
热议问题