Skip to content

Commit

Permalink
Product Collection: Remove best selling filter option (woocommerce#51865
Browse files Browse the repository at this point in the history
)

* Remove best selling filter option

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* Add tiebreak fallback in case total_sales is 0 which prevents random order

* Remove test_merging_order_by_popularity_queries as it is not needed

---------

Co-authored-by: github-actions <github-actions@github.com>
  • Loading branch information
roykho and github-actions authored Oct 6, 2024
1 parent 72ab844 commit 7542487
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ const orderOptions = [
label: __( 'Sales, low to high', 'woocommerce' ),
value: 'sales/asc',
},
{
value: 'popularity/desc',
label: __( 'Best Selling', 'woocommerce' ),
},
{
value: 'rating/desc',
label: __( 'Top Rated', 'woocommerce' ),
Expand All @@ -73,6 +69,13 @@ const OrderByControl = ( props: QueryControlProps ) => {
trackInteraction( CoreFilterNames.ORDER );
};

let orderValue = `${ orderBy }/${ order }`;

// This is to provide backward compatibility as we removed the 'popularity' (Best Selling) option from the order options.
if ( orderBy === 'popularity' ) {
orderValue = `sales/${ order }`;
}

return (
<ToolsPanelItem
label={ __( 'Order by', 'woocommerce' ) }
Expand All @@ -85,7 +88,7 @@ const OrderByControl = ( props: QueryControlProps ) => {
resetAllFilter={ deselectCallback }
>
<SelectControl
value={ `${ orderBy }/${ order }` }
value={ orderValue }
options={ orderOptions }
label={ __( 'Order by', 'woocommerce' ) }
onChange={ ( value ) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: update

Remove "Best Selling" filters option in favor of "Sales, high to low" for Product Collection block
10 changes: 6 additions & 4 deletions plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,8 @@ private function get_custom_orderby_query( $orderby ) {
);
}

if ( 'sales' === $orderby ) {
// The popularity orderby value here is for backwards compatibility as we have since removed the filter option.
if ( 'sales' === $orderby || 'popularity' === $orderby ) {
add_filter( 'posts_clauses', array( $this, 'add_sales_sorting_posts_clauses' ), 10, 2 );
return array(
'isProductCollection' => true,
Expand All @@ -1073,8 +1074,7 @@ private function get_custom_orderby_query( $orderby ) {
}

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

return array(
Expand Down Expand Up @@ -1791,7 +1791,9 @@ public function add_sales_sorting_posts_clauses( $clauses, $query ) {
}

$orderby = $query_vars['orderby'] ?? null;
if ( 'sales' !== $orderby ) {

// The popularity orderby value here is for backwards compatibility as we have since removed the filter option.
if ( 'sales' !== $orderby && 'popularity' !== $orderby ) {
return $clauses;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,6 @@ public function test_merging_order_by_rating_queries() {
$this->assertEquals( '_wc_average_rating', $merged_query['meta_key'] );
}

/**
* Test merging order by popularity queries.
*/
public function test_merging_order_by_popularity_queries() {
$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['orderBy'] = 'popularity';

$merged_query = $this->initialize_merged_query( $parsed_block );

$this->assertEquals( 'meta_value_num', $merged_query['orderby'] );
$this->assertEquals( 'total_sales', $merged_query['meta_key'] );
}

/**
* Test product visibility query exist in merged query.
*/
Expand Down

0 comments on commit 7542487

Please sign in to comment.