通过woocommerce评级对产品进行排序

我正试图通过平均评级在Woocommerce的产品类别页面上对产品进行分类 .

我找到了将评级放在产品页面上的代码,将代码添加到functions.php(见下文),但选择评级没有做任何事情 .

我需要调用(或创建)才能使其工作的meta_key是什么?

add_filter('woocommerce_get_catalog_ordering_args','custom_woocommerce_get_catalog_ordering_args');

function custom_woocommerce_get_catalog_ordering_args( $args ) {    
    if (isset($_SESSION['orderby'])) {

        switch ($_SESSION['orderby']) :

            case 'date_asc' :

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

                $args['order'] = 'asc';

                $args['meta_key'] = '';

            break;

            case 'price_desc' :

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

                $args['order'] = 'desc';

                $args['meta_key'] = '_price';

            break;

            case 'title_desc' :

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

                $args['order'] = 'desc';

                $args['meta_key'] = '';

            break;

            case 'rating_desc' :

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

                $args['order'] = 'desc';

                $args['meta_key'] = '';

            break;

        endswitch;

    }

    return $args;    
}

add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');

function custom_woocommerce_catalog_orderby( $sortby ) {            
    $sortby['rating_desc'] = 'Rating';
    $sortby['title_desc'] = 'Reverse-Alphabetically';

    $sortby['price_desc'] = 'Price (highest to lowest)';

    $sortby['date_asc'] = 'Oldest to newest';

    return $sortby;    
}

回答(1)

3 years ago

尝试将您的案例'rating_desc'更改为此

case 'rating_desc' :

    add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );

break;

当我遇到类似的问题时,在第492行之后发现那段代码包含了\ class-wc-query.php . 如果您需要核心功能order_by_rating_post_clauses,它位于第547行的同一文件中 .

希望能帮助到你!