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

Product Query: Add order by “Top rated” as a preset #7715

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const PRESETS = [
key: 'popularity/desc',
name: __( 'Best Selling', 'woo-gutenberg-products-block' ),
},
{
key: 'rating/desc',
name: __( 'Top Rated', 'woo-gutenberg-products-block' ),
},
];

export function PopularPresets( props: ProductQueryBlock ) {
Expand Down
7 changes: 5 additions & 2 deletions src/BlockTypes/ProductQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ProductQuery extends AbstractBlock {
*
* @var array
*/
protected $custom_order_opts = array( 'popularity' );
protected $custom_order_opts = array( 'popularity', 'rating' );

/**
* All the query args related to the filter by attributes block.
Expand Down Expand Up @@ -209,7 +209,9 @@ private function merge_queries( $a, $b ) {
* @return array
*/
public function extend_rest_query_allowed_params( $params ) {
$params['orderby']['enum'] = array_merge( $params['orderby']['enum'], $this->custom_order_opts );
$original_enum = isset( $params['orderby']['enum'] ) ? $params['orderby']['enum'] : array();

$params['orderby']['enum'] = array_merge( $original_enum, $this->custom_order_opts );
return $params;
}

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

$meta_keys = array(
'popularity' => 'total_sales',
'rating' => '_wc_average_rating',
);

return array(
Expand Down