Woocommerce:按自定义分类排序 [英] Woocommerce: order by custom taxonomy

查看:44
本文介绍了Woocommerce:按自定义分类排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下问题.我已经使用 WooCommerce 建立了一个 Wordpress 网站,以便为仅提供书籍的网上商店提供服务.

我创建了一些基于分类法的产品属性,例如publisher"和author"(因为多个产品可以共享一个作者或一个出版商)

我希望不仅能够在 Woocommerce 默认字段(如标题"和价格")上而且在这些分类字段上对我的产品进行排序.比如说:按作者 ASC 订购按出版商 DESC 订购

据我所知,Wordpress 的核心功能无法做到这一点.有人说这是因为按分类法字段排序没有意义,但是对于上面的示例,我无法理解为什么您不想按例如作者排序.

我玩过 sortby meta_value,但这只是查询直接 postmeta,而不是分类法.

我了解 php,因此任何涉及我的 functions.php 文件中的附加代码的解决方案都可以.

解决方案

我找到了解决这个问题的方法:https://gist.github.com/jayarnielsen/12f3a586900aa6759639

我稍微修改了代码,所以它等于 Woocommerce 用来按系统字段title"和price"排序的方式,它添加了一个名为sortby"的 url 查询参数,其结构如下:field"-方向"因此,要按名为pa_tax1"的产品属性升序排序,您需要添加到 url:sortby=pa_tax1-asc

add_filter('posts_clauses', 'posts_clauses_with_tax', 10, 2);函数posts_clauses_with_tax( $clauses, $wp_query ) {全球 $wpdb;//要排序的分类法数组//如果是 Woocommerce Properties,请务必添加 pa_ 前缀$taxonomies = array('pa_tax1', 'pa_tax2', 'pa_tax3');//如果没有找到orderby查询参数,什么都不做if(!isset($wp_query->query['orderby'])) {返回 $ 子句;}//分解 orderby 查询参数$orderQuery = expand('-', $wp_query->query['orderby']);$orderBy = [];$orderBy['field'] = $orderQuery[0];$orderBy['direction'] = (isset($orderQuery[1])) ?strtoupper($orderQuery[1]) : 'ASC';//只添加子句,如果 sortby 字段在我们的数组中if( in_array($orderBy['field'], $taxonomies) ) {$clauses['join'] .= "LEFT OUTER JOIN {$wpdb->term_relationships} AS rel2 ON {$wpdb->posts}.ID = rel2.object_idLEFT OUTER JOIN {$wpdb->term_taxonomy} AS tax2 ON rel2.term_taxonomy_id = tax2.term_taxonomy_idLEFT OUTER JOIN {$wpdb->terms} 使用 (term_id)";$clauses['where'] .= " AND (taxonomy = '".$orderBy['field']."' OR taxonomy IS NULL)";$clauses['groupby'] = "rel2.object_id";$clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC)";$clauses['orderby'] .= ( 'ASC' == strtoupper( $orderBy['direction'] ) ) ?'升序':'降序';返回 $ 子句;}别的 {返回 $ 子句;}}

I'm having the following problem. I have set up a Wordpress site with WooCommerce to serve a webshop with only books.

I have created some product attributes that are based on taxonomies like 'publisher' and 'author' (as multiple products can share an author or a publisher)

I would like to be able to sort my products not only on the Woocommerce default fields like 'title' and 'price' but also on these taxonomy fields. Say for example: order by Author ASC or order by publisher DESC

As far as I have discovered there is no way to do this with Wordpress core functions. Some say this is because it makes no sense to sort by taxonomy fields, but with the above example I can't understand why you don't want to sort by for example author.

I have played around with sortby meta_value, but this is just querying the direct postmeta, not the taxonomies.

I have knowledge of php, so any solutions involving additional code in my functions.php file will do.

解决方案

I found out a way how to solve this: https://gist.github.com/jayarnielsen/12f3a586900aa6759639

I slightly modified the code so it equals the way Woocommerce uses to sort by the system fields "title" and "price" which is adding a url query-param named "sortby" a value structured like this: "field"-"direction" So to sort by a product property called "pa_tax1" ascending you'll need to add to the url: sortby=pa_tax1-asc

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

    // Array of taxonomies you want to sort on
    // in case of Woocommerce Properties, be sure to add the pa_ prefix
    $taxonomies = array('pa_tax1', 'pa_tax2', 'pa_tax3');

    // If no orderby query param is found, do nothing
    if( !isset($wp_query->query['orderby']) ) {
        return $clauses;
    }

    // Explode the orderby query-param
    $orderQuery = explode('-', $wp_query->query['orderby']);
    $orderBy = [];
    $orderBy['field'] = $orderQuery[0];
    $orderBy['direction'] = (isset($orderQuery[1])) ? strtoupper($orderQuery[1]) : 'ASC';

    // Only add clauses, if the sortby field is in our array
    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}.name ORDER BY name ASC) ";
        $clauses['orderby'] .= ( 'ASC' == strtoupper( $orderBy['direction'] ) ) ? 'ASC' : 'DESC';

        return $clauses;
    }
    else {
        return $clauses;
    }
}

这篇关于Woocommerce:按自定义分类排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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