我想按受欢迎程度(仅是标准设置)对我的整个WooCommerce目录进行排序,但我也希望按日期对我的特价商品类别进行排序,因为我们将在一周中的特定日期发布新交易。
我在这里找到了这段代码,但是它似乎根本没有改变特殊类别的顺序:
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 20, 1 );
function custom_catalog_ordering_args( $args ) {
$product_category = 'specials'; // <== HERE define your product category
// Only for defined product category archive page
if( ! is_product_category($product_category) ) return $args;
// Set default ordering to 'date ID', so "Newness"
$args['orderby'] = 'date ID';
if( $args['orderby'] == 'date ID' )
$args['order'] = 'DESC'; // Set order by DESC
return $args;
}
如何获得所需的订单?
答案 0 :(得分:1)
您使用了错误的钩子,woocommerce_get_catalog_ordering_args
用于添加不需要的新排序选项,因为WooCommerce已经具有最新的sortby选项。
因此,为了修改特定类别的默认排序选项,您需要使用woocommerce_default_catalog_orderby
钩子,如下所示:
add_filter('woocommerce_default_catalog_orderby', 'custom_catalog_ordering_args', 20, 1);
function custom_catalog_ordering_args($sortby)
{
$product_category = 'specials'; // <== HERE define your product category slug
// Only for defined product category archive page
if (! is_product_category($product_category)) {
return;
}
return 'date';
}
上面的代码已经过测试,可以正常工作,只需将代码添加到functions.php
中,就可以开始使用