duanqiongdu9916 2018-04-17 17:27
浏览 329
已采纳

更改特定Woocommerce产品类别存档页面的默认排序

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.

  • 写回答

2条回答 默认 最新

  • dtiopy6088 2018-04-17 20:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示