在 Woocommerce 中按品牌和标题对产品进行排序 [英] Sort products by brand and title in Woocommerce

查看:73
本文介绍了在 Woocommerce 中按品牌和标题对产品进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Woocommerce 中,我目前正在尝试更改默认的 orderby 以显示按品牌和标题排序的产品.

In Woocommerce I'm currently trying to change the default orderby in to show products sorted by their brand and title.

品牌是通过完美的 Woocommerce 品牌完成的.我已经设法根据品牌 slug 获得订单(因为我就是这样设置他们的订单).我还使用 WOOF(Woocommerce 产品过滤器)插件按类别/品牌过滤.

The brands are done via perfect Woocommerce brands. 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.

我正在按分类法对产品进行分类 wpb-brand 并且我正在寻找一种按产品标题进行二次排序的方法,但我不知道如何我可以做到这一点.

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.

这是我的代码:

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

有什么办法可以让产品对象添加第二个 orderby 子句?

Is there any way I could get the products object to add a second orderby clause?

推荐答案

能够解决问题.

就像注释掉最后的 'orderby' 行一样简单,因为它有点多余,并通过数据库对象获取帖子表.

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.

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

这篇关于在 Woocommerce 中按品牌和标题对产品进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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