更改特定Woocommerce产品类别存档页面的默认排序 [英] Change default sorting for specific Woocommerce product category archive pages

查看:373
本文介绍了更改特定Woocommerce产品类别存档页面的默认排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将网站上特定产品类别的默认产品分类选项更改为新".我知道您可以转到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');

我对排序功能还没有做很多事情,所以我不确定从哪里开始.我知道该功能应该放在子主题的functions.php中.

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;
}

代码进入您的活动子主题(或活动主题)的function.php文件中..经过测试,可以正常工作.

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

这篇关于更改特定Woocommerce产品类别存档页面的默认排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆