Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Incorrect result when WooCommerce settings Default product sorting set to Popularity #3617

Closed
1 task done
burhandodhy opened this issue Aug 25, 2023 · 1 comment · Fixed by #3619
Closed
1 task done
Assignees
Labels
bug Something isn't working
Milestone

Comments

@burhandodhy
Copy link
Contributor

burhandodhy commented Aug 25, 2023

Describe the bug

When "Default product sorting" settings is set to "Popularity". The shop page doesn't return the popular products. The problem is that the order in a query is set to asc

    "sort": [
        {
            "meta.total_sales.double": {
                "order": "asc"
            }
        },
        {
            "post_date": {
                "order": "asc"
            }
        }
    ]

This issue caused by this change in this snippet https://github.com/10up/ElasticPress/blob/develop/includes/classes/Feature/WooCommerce/Products.php#L919 compare to what 4.6.0 has https://github.com/10up/ElasticPress/blob/4.6.0/includes/classes/Feature/WooCommerce/WooCommerce.php#L516

Steps to Reproduce

  1. Set the "Default product sorting" settings set to "Popularity"
  2. Go to shop page. The popular post wouldn't be listed
  3. Select any other sorting option.
  4. Again, select the Popularity and this time it will display the correct result.

Screenshots, screen recording, code snippet

No response

Environment information

No response

WordPress and ElasticPress information

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@burhandodhy burhandodhy added the bug Something isn't working label Aug 25, 2023
@felipeelia felipeelia modified the milestones: 5.0.0, 4.7.1 Aug 28, 2023
@felipeelia
Copy link
Member

While #3619 isn't merged and released, adding this snippet to functions.php fixes the issue:

/**
 * Fix WooCommerce Products ordering by popularity in ElasticPress 4.7.0
 *
 * @param \WP_Query $query The WP Query
 */
function ep_custom_woocommerce_popularity_order( $query ) {
	if ( ! defined( 'EP_VERSION' ) || '4.7.0' !== EP_VERSION ) {
		return;
	}

	$orderby = $query->get( 'orderby', null );
	if ( 'popularity' !== $orderby ) {
		return;
	}

	$query->set( 'order', 'DESC' );
}
add_action( 'pre_get_posts', 'ep_custom_woocommerce_popularity_order', 10 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants