Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 9295dde

Browse files
Product Query: Add order by “Top rated” as a preset (#7715)
* Add safety guard to `extend_rest_query_allowed_params`
1 parent b781afe commit 9295dde

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

assets/js/blocks/product-query/inspector-controls/popular-presets.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ const PRESETS = [
1616
key: 'popularity/desc',
1717
name: __( 'Best Selling', 'woo-gutenberg-products-block' ),
1818
},
19+
{
20+
key: 'rating/desc',
21+
name: __( 'Top Rated', 'woo-gutenberg-products-block' ),
22+
},
1923
];
2024

2125
export function PopularPresets( props: ProductQueryBlock ) {

src/BlockTypes/ProductQuery.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ProductQuery extends AbstractBlock {
2828
*
2929
* @var array
3030
*/
31-
protected $custom_order_opts = array( 'popularity' );
31+
protected $custom_order_opts = array( 'popularity', 'rating' );
3232

3333
/**
3434
* All the query args related to the filter by attributes block.
@@ -209,7 +209,9 @@ private function merge_queries( $a, $b ) {
209209
* @return array
210210
*/
211211
public function extend_rest_query_allowed_params( $params ) {
212-
$params['orderby']['enum'] = array_merge( $params['orderby']['enum'], $this->custom_order_opts );
212+
$original_enum = isset( $params['orderby']['enum'] ) ? $params['orderby']['enum'] : array();
213+
214+
$params['orderby']['enum'] = array_merge( $original_enum, $this->custom_order_opts );
213215
return $params;
214216
}
215217

@@ -238,6 +240,7 @@ private function get_custom_orderby_query( $orderby ) {
238240

239241
$meta_keys = array(
240242
'popularity' => 'total_sales',
243+
'rating' => '_wc_average_rating',
241244
);
242245

243246
return array(

0 commit comments

Comments
 (0)