无法将特定 woocommerce 类别的默认排序顺序更改为 'popularity' [英] Cant change default sorting order of specific woocommerce category to 'popularity'

查看:17
本文介绍了无法将特定 woocommerce 类别的默认排序顺序更改为 'popularity'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试更改一个特定 woocommerce 类别的默认排序顺序.

Hello I am trying to change the default sorting order for one specific woocommerce category.

这个类别有一些 cbd-best-sellers

This category has a slug of cbd-best-sellers

我正在尝试将此类别的默认排序顺序更改为按受欢迎程度".

I am trying to change the default sort order to "by popularity" for this category.

我发现以下代码将特定类别的默认排序更改为按日期"

I found this below code which changes the default sorting to "by date" for a specific category

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

我用我的类别 slug cbd-best-sellers 替换了 T 恤,并将日期 ID 更改为受欢迎程度,如下所示:

I than replaced t-shirts with my category slug cbd-best-sellers and changed date ID to popularity like so:

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 20, 1 );
function custom_catalog_ordering_args( $args ) {
    $product_category = 'cbd-best-sellers'; // <== HERE define your product category

    if( ! is_product_category($product_category) ) return $args;

    $args['orderby'] = 'popularity';

    if( $args['orderby'] == 'popularity' )
        $args['order'] = 'ASC'; // Set order by DESC

    return $args;
}

但该类别仍未按加载时的受欢迎程度排序.

But the category is still not sorting by popularity on load.

我做错了吗?

推荐答案

您使用的钩子用于添加或更改值的顺序,而不是设置默认值.

the hook you are using is for adding or changing the order by values not to set the default one.

如果你想设置默认排序选项,你需要使用woocommerce_default_catalog_orderby

If you want to set default sorting option you need to use woocommerce_default_catalog_orderby

所以你的代码应该如下所示:

so your code should be like the following:

add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' );

function custom_default_catalog_orderby() {

    $product_category = 'cbd-best-sellers'; // <== HERE define your product category

    if ( is_product_category( $product_category ) ) {
        return 'popularity'; // Can also use title and price
    }

}

这篇关于无法将特定 woocommerce 类别的默认排序顺序更改为 &amp;#39;popularity&amp;#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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