Php 在商业中按品牌和价格对产品进行分类
当我试图通过修改来复制这里找到的解决方案()时,我遇到了一块骨头 第一种按品牌分类的方法很有魅力,但是我找不到一种方法通过增加或降低价格来进行第二种分类Php 在商业中按品牌和价格对产品进行分类,php,sorting,woocommerce,product,price,Php,Sorting,Woocommerce,Product,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'); $ord
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;
}
}
我已经找到了这些行,但是我很难在上面的查询中添加这些行来用价格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;
你对解决方案有什么想法吗?好的,总是在我们写问题和发展时,答案会在后面出现 以下是使用插件在WooCommerce中添加自定义过滤器品牌和价格ASC的完整解决方案 首先,添加自定义过滤器:
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;
}
然后再加上
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;
}
最后
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;
}
}