Wordpress Woocommerce按类别对产品进行排序的方式有所不同 [英] Wordpress Woocommerce sort products differently per category
问题描述
我正在与wordpress和woocommerce合作.我最近一直在尝试并搜索的是一种按产品类别对产品进行排序的方法.例如,汽车"类别中的产品必须按价格ASC排序.我大约有10种不同的类别,具有不同的排序方式.我认为下面的这段代码可以解决我的问题,但事实并非如此.我尝试了多种方法,但是找不到正确的方法.我的PHP知识非常有限,但是每天都要学习.我应该用例吗?我将如何完成这项工作?也许这只是另一种写作方式?我希望有人可以帮助我.预先感谢.
I am working with wordpress and woocommerce. What I have been trying and search for the last days is a way to sort products per productcategory. Products in category 'cars' for instance must be sorted by price ASC. I have about 10 different categories with different sorting. I thought this code below would solve my problem, but it doest. I have tried multiple ways, but can't find the right one. My PHP knowledge is very limited, but learning everyday. Shoud I use case ? How would I accomplish this ? Or maybe it's just a other way of writing ? I hope someone can assist me. Thanks in advance.
Bas
add_filter('woocommerce_get_catalog_ordering_args','tk_woocommerce_catalog_orderbyb');
function tk_woocommerce_catalog_orderbyb( $args ) {
$cat = get_the_terms( $product->ID, 'product_cat' );
foreach ($cat as $categoria) {
if($categoria->parent == 0) {
$main_parent = $categoria->name;
}
}
if($main_parent == 'Cars') {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = '_price';
}
return $args;
}
推荐答案
我知道这不是一个很干净的解决方案,但是它可以工作.使用'else if'语句(或case),我可以对每个产品类别使用不同的排序方式.
I know this is not a very clean solution, but it works. And with an 'else if' statement ( or with case ) I can use different sorting per product category.
add_filter('woocommerce_get_catalog_ordering_args', 'tk_woocommerce_catalog_orderbyb');
function tk_woocommerce_catalog_orderbyb($args) {
$urlb = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($urlb,'cars')) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = '_price';
}
return $args;
}
这篇关于Wordpress Woocommerce按类别对产品进行排序的方式有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!