在商店里,有些产品属于多个类别。这使得默认的商业订购不起作用,因为如果一个产品在类别A中排名第一,那么另一个产品就不能在类别B中排名第一,如果两者都属于同一类别。
我想找到一种方法,按我想要的特定顺序(我将提供ID)按帖子ID对商店类别中的产品进行排序。
到目前为止,我想出了:
add_filter( 'woocommerce_get_catalog_ordering_args', 'my_ordering_function', 20, 1 );
function my_ordering_function( $args ) {
$product_category = 'my-category';
if( ! is_product_category($product_category) ) return $args;
$args['post__in'] = array( 74, 116, 3565, 3563 );
$args['orderby'] = 'post__in';
$args['order'] = 'ASC';
return $args;
}
这段代码应该可以完成这项工作,但是在检查了woocommerce_get_catalog_ordering_args
源代码后,看起来post__in甚至不受支持。
关于如何规定每个类别的我自己的产品订单,有什么想法吗?
使用您的方法,您可以在使用woocommerce_get_catalog_ordering_args
过滤器的函数的父函数中执行此操作。
您正在寻找的函数是product\u query
,在该函数中,您有一个操作挂钩woocommerce\u product\u query
。对于您的用例,类似这样的东西会起作用。
function my_ordering_function( $q, $this ) {
$q->set( 'post__in', array( 74, 116, 3565, 3563 ) );
$q->set( 'orderby', 'post__in' );
}
add_action( 'woocommerce_product_query', 'my_ordering_function', 10, 2 );
另外,此函数也位于您引用的同一文件中。你可以在那里看看它是如何工作的。