似乎有很多元密钥可以用来订购(here 是我找到的列表)。然而,我找不到任何与评级相关的内容。我搜索了rating
在WooCommerce源代码中找到以下行:includes/abstracts/abstract-wc-product.php#L1087
下面是在使用WooCommerce PHP REST API客户端的环境中最终对我起作用的内容:
function getProducts(WP_REST_Request $request) {
global $wc_api_client;
$category = $request->get_param(\'category\');
// $filters = $request->get_param(\'filters\');
$orderby = $request->get_param(\'orderby\');
$args = null;
$wc_query = new WC_Query();
switch ( $orderby ) {
case \'date\':
$args = array(
\'filter[limit]\' => 6,
\'filter[category]\' => $category,
\'filter[orderby]\' => \'date\',
\'filter[order]\' => \'DESC\'
);
break;
case \'price\':
$args = array(
\'filter[limit]\' => 6,
\'filter[category]\' => $category,
\'filter[orderby]\' => \'meta_value_num\',
\'filter[order]\' => \'ASC\',
\'filter[orderby_meta_key]\' => \'_price\'
);
break;
case \'price-desc\':
$args = array(
\'filter[limit]\' => 6,
\'filter[category]\' => $category,
\'filter[orderby]\' => \'meta_value_num\',
\'filter[order]\' => \'DESC\',
\'filter[orderby_meta_key]\' => \'_price\'
);
break;
case \'popularity\':
$args = array(
\'filter[limit]\' => 6,
\'filter[category]\' => $category,
\'filter[orderby]\' => \'meta_value_num\',
\'filter[order]\' => \'DESC\',
\'filter[orderby_meta_key]\' => \'total_sales\'
);
break;
case \'rating\':
$args = array(
\'filter[limit]\' => 6,
\'filter[category]\' => $category,
\'filter[orderby]\' => \'meta_value_num\',
\'filter[order]\' => \'DESC\',
\'filter[orderby_meta_key]\' => \'_wc_average_rating\'
);
break;
default:
$args = array(
\'filter[limit]\' => 6,
\'filter[category]\' => $category
);
}
try {
$raw = $wc_api_client->products->get(\'\', $args);
return $raw[\'products\'];
} catch (Exception $e) {
return new WP_Error(\'no_products\', \'Couldn’t find any products\', array(\'status\' => 404));
}
}