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

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

问题描述

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

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

此类别有很多cbd畅销产品

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

然后我将T恤替换为"slug cbd-best-best-sellers"类别,并将日期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类别的默认排序顺序更改为“受欢迎程度"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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