简体   繁体   English

在 Woocommerce 中按品牌和价格对产品进行排序

[英]Sort products by brand and price in Woocommerce

While trying to reproduce the solution found here ( Sort products by brand and title in Woocommerce ) by modifying it, I came across a bone...在尝试通过修改来重现此处找到的解决方案( 在 Woocommerce 中按品牌和标题排序产品)时,我遇到了一个骨头......

The first sort by brand works like a charm however I cannot find a way to do a second sort by increasing or decreasing price.第一种按品牌排序就像一种魅力,但是我找不到通过增加或降低价格来进行第二种排序的方法。

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'] .= ", {$wpdb->posts}.post_title ASC";
    return $clauses;
}
else {
    return $clauses;
}
}

I've found those lines but it's very hard for me to add those lines in the query above to replace the orderby title by price ASC我找到了这些行,但我很难在上面的查询中添加这些行以按价格 ASC 替换 orderby 标题

$clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} price ON( {$wpdb->posts }.ID = price.post_id AND price.meta_key = 'price') ";
$clauses['orderby'] = " CONVERT( REPLACE(price.meta_value, '$', ''), DECIMAL(13,2) ) " . $order;

Do you have any idea of the solution?您对解决方案有任何想法吗?

Ok it's always when we write the question and develop that the answer comes later...好吧,当我们写下问题并提出答案时,总是会在稍后出现......

Here is the complete solution to add a custom filter brand and price ASC in your WooCommerce by using the plugin Perfect Brands for WooCommerce这是使用 WooCommerce 插件完美品牌在 WooCommerce中添加自定义过滤器品牌和价格 ASC 的完整解决方案

First, add the custom filter:首先,添加自定义过滤器:

add_filter( 'woocommerce_catalog_orderby', 'my_add_custom_sorting_options' );
add_filter( 'woocommerce_default_catalog_orderby_options',  'my_add_custom_sorting_options' );
function my_add_custom_sorting_options( $options ){
    $options['brands'] = __("Tri par marque / prix croissant", "yourdomain");
    return $options;
}

And than add而不是添加

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'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    if ( 'brands' == $orderby_value ) {
        add_filter('posts_clauses', 'posts_clauses_brands_price', 10, 2);
    }
    return $args;
}

Finally最后

function posts_clauses_brands_price( $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 JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id ";
        $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'] .= ', wc_product_meta_lookup.min_price ASC, wc_product_meta_lookup.product_id ASC ';

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

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

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