WooCommerce 中的多个订单 [英] Multiple order by in WooCommerce
本文介绍了WooCommerce 中的多个订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按平均评分 (DESC) 和价格 (ASC) 在类别页面中订购我的产品.
I want to order my products in category page by average rating (DESC) and then by price (ASC).
| id | avgrating | price |
| 1 | 4 | 10 |
| 2 | 4 | 5 |
| 3 | 5 | 7 |
顺序:3、2、1.
所以我尝试了:
$args['meta_key'] = '_wc_average_rating';
$args['orderby'] = array(
'meta_value_num' => 'DESC',
'price' => 'ASC',
);
但它们(也)不是按价格订购的.我也用 _price
替换了 price
,结果相同.
But they aren't ordered (also) by price. I also replaced price
with _price
, same result.
我使用的是最新版本的 WordPress (4.8) 和 WooCommerce (3.0.8).
I'm using latest version of WordPress (4.8) and WooCommerce (3.0.8).
如果我使用:
$args['meta_key'] = '_wc_average_rating';
$args['orderby'] = array(
'meta_value_num' => 'DESC',
'ID' => 'DESC',
);
Order 以例外方式工作,按平均评分 DESC,然后按 ID DESC.所以,我必须用 price
更改 ID
但我不能让它工作.
Order works as excepted, DESC by average rating and then DESC by ID. So, I have to change ID
with price
but I can't make it work.
推荐答案
通过传递带有参数的 order
进行检查
Check by passing the order
with arguments
检查这些,
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'sort_by_type' == $orderby_value ) {
$args['orderby'] = 'meta_value_num title';
$args['order'] = 'ASC';
$args['meta_key'] = 'sort_by_type';
}
if ( '_wc_average_rating' == $orderby_value ) {
$args['orderby'] = 'meta_value_num title';
$args['order'] = 'DESC';
$args['meta_key'] = '_wc_average_rating';
}
return $args;
}
这篇关于WooCommerce 中的多个订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文