简体   繁体   English

在 Woocommerce 中按品牌和标题对产品进行排序

[英]Sort products by brand and title in Woocommerce

In Woocommerce I'm currently trying to change the default orderby in to show products sorted by their brand and title.在 Woocommerce 中,我目前正在尝试更改默认的orderby以显示按品牌和标题排序的产品。

The brands are done via perfect Woocommerce brands.这些品牌是通过完美的 Woocommerce 品牌完成的。 I've managed to get the ordering according to the brand slug (as that is how I set their order).我已经设法根据品牌标签获得了订单(因为我就是这样设置他们的订单的)。 I'm also using WOOF (Woocommerce products filter) plugin to filter by category/brand.我还使用 WOOF(Woocommerce 产品过滤器)插件按类别/品牌进行过滤。

I'm sorting products by the taxonomy wpb-brand and I'm looking for a way to secondary sort by product title but I'm not aware of how I can do this.我正在按分类法wpb-brand对产品进行排序,我正在寻找一种按产品标题进行二次排序的方法,但我不知道如何执行此操作。

This is my code:这是我的代码:

add_filter('posts_clauses', 'posts_clauses_with_tax', 10, 2);
function posts_clauses_with_tax( $clauses, $wp_query ) {
global $wpdb;

$taxonomies = array('pwb-brand');

$orderBy['field'] = "pwb-brand";
$orderBy['direction'] = "ASC";

if( in_array($orderBy['field'], $taxonomies) ) {
    $clauses['join'] .= "
        LEFT OUTER JOIN {$wpdb->term_relationships} AS rel2 ON {$wpdb->posts}.ID = rel2.object_id
        LEFT OUTER JOIN {$wpdb->term_taxonomy} AS tax2 ON rel2.term_taxonomy_id = tax2.term_taxonomy_id
        LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
    ";

    $clauses['where'] .= " AND (taxonomy = '".$orderBy['field']."' OR taxonomy IS NULL)";
    $clauses['groupby'] = "rel2.object_id";
    $clauses['orderby']  = "GROUP_CONCAT({$wpdb->terms}.slug ORDER BY slug ASC) ";
    $clauses['orderby'] .= ( 'ASC' == strtoupper( $orderBy['direction'] ) ) ? 'ASC' : 'DESC';
    return $clauses;
}
else {
    return $clauses;
}
}

Is there any way I could get the products object to add a second orderby clause?有什么方法可以让产品对象添加第二个orderby子句?

Was able to solve the issue.能够解决问题。

It was as simple as commenting out the last 'orderby' line as it was somewhat redundant and get the posts table via the database object.它就像注释掉最后一个“orderby”行一样简单,因为它有点多余,并通过数据库对象获取帖子表。

//$clauses['orderby'] .= ( 'ASC' == strtoupper( $orderBy['direction'] ) ) ? 'ASC' : 'DESC';
$clauses['orderby'] .= ", {$wpdb->posts}.post_title ASC"; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM