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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据