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条)

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图