我正在尝试选择按menu_order +受欢迎程度对WooCommerce产品进行排序。
因此,产品将按menu_order排序,如果它们具有相同的menu_order,则它们将按受欢迎程度显示
我认为这个简单的代码会起作用,但事实并非如此:
export function findIfMatches(check: (component: any) => boolean): (queryDebugElement: DebugElement) => boolean {
return (queryDebugElement: DebugElement) => {
if (queryDebugElement.componentInstance) {
if (queryDebugElement.componentInstance.id === id) {
return check(queryDebugElement.componentInstance);
}
}
return false;
};
}
const firstComponent = debugElement.query(findIfMatches(component => component.id === 'first-component'));
const secondComponent = debugElement.query(findIfMatches(component => component.id === 'second-component'));
能做到吗?
答案 0 :(得分:0)
请,请考虑通过多个自变量进行排序。
$args['orderby'] = [
'popularity_clause' => 'DESC',
'menu_order' => 'DESC',
];
$args['meta_query'][] = [
'relation' => 'OR',
'popularity_clause' => [
'key' => 'total_sales',
'compare' => '<=',
],
];
这是按元查询值产品具有(total_sales
)的排序-受欢迎程度。如果total_sales相同,则按menu_order。
答案 1 :(得分:0)
我无法使Andriy Kovalenko代码正常工作,也不知道这是否是我的错,但是无论如何,此代码似乎仍然有效:
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 ('recommended' == $orderby_value) {
$args['orderby'] = ['menu_order' => 'DESC', 'meta_value_num' => 'DESC'];
$args['meta_key'] = 'total_sales';
}
return $args;
}