dongxiong1935 2017-06-22 05:22 采纳率: 0%
浏览 62
已采纳

WooCommerce中的多个订单

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    |

Order: 3, 2, 1.

So I tried with:

$args['meta_key'] = '_wc_average_rating';
$args['orderby']  = array(
    'meta_value_num' => 'DESC',
    'price' => 'ASC',
);

But they aren't ordered (also) by price. I also replaced price with _price, same result.

I'm using latest version of WordPress (4.8) and WooCommerce (3.0.8).

Edit:

If I use:

$args['meta_key'] = '_wc_average_rating';
$args['orderby']  = array(
    'meta_value_num' => 'DESC',
    'ID'             => 'DESC',
);

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.

  • 写回答

1条回答 默认 最新

  • dongying6896 2017-06-22 06:17
    关注

    Check by passing the order with arguments

    Check with these,

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图