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: No mapping found for [popularity] in order to sort on #3535

Closed
1 task done
felipeelia opened this issue Jul 7, 2023 · 3 comments · Fixed by #3546
Closed
1 task done

BUG: No mapping found for [popularity] in order to sort on #3535

felipeelia opened this issue Jul 7, 2023 · 3 comments · Fixed by #3546
Assignees
Milestone

Comments

@felipeelia
Copy link
Member

Describe the bug

Under certain circumstances for WooCommerce stores, ElasticPress will integrate a WP_Query that is not the main query, causing an error No mapping found for [popularity] in order to sort on

Steps to Reproduce

Place the following snippet anywhere:

$wc_query = new \WP_Query(
	[
		'post_type'   => 'product',
		'orderby'     => 'popularity',
		'product_cat' => 'test',
	]
);

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
@felipeelia felipeelia added the bug Something isn't working label Jul 7, 2023
@felipeelia
Copy link
Member Author

This is fixed in #3502 (see 6834542)

In the meantime, users facing this problem can drop the following snippet in a custom plugin or their theme's functions.php file:

/**
 * Translate WooCommerce `price` and `popularity` fields in orderby clauses.
 *
 * This should be removed if using versions of ElasticPress newer than 4.6.1
 *
 * @see https://github.com/10up/ElasticPress/issues/3535
 * @param \WP_Query $query The WP Query
 */
function ep_custom_fix_woocommerce_fields_sort_translation( $query ) {
	if ( ! is_plugin_active( 'elasticpress/elasticpress.php' ) || ! defined( 'EP_VERSION' ) ) {
		return;
	}

	if ( version_compare( EP_VERSION, '4.6.1', '>' ) ) {
		_deprecated_function( __METHOD__, 'EP 4.6.1', 'This function is not needed anymore.' );
		return;
	}

	$orderby = $query->get( 'orderby', '' );
	if ( ! in_array( $orderby, [ 'price', 'popularity' ], true ) ) {
		return;
	}

	$orderby_field = ( 'price' === $orderby ) ?
		'meta._price.double date' :
		'meta.total_sales.double date';
	$query->set( 'orderby', $orderby_field );
}
add_action( 'pre_get_posts', 'ep_custom_fix_woocommerce_fields_sort_translation' );

@felipeelia
Copy link
Member Author

felipeelia commented Jul 7, 2023

The underlying issue (why these queries are integrated) is here:

	public function should_integrate_with_query( \WP_Query $query ) : bool {
		/**
		 * Check for taxonomies
		 */
		$supported_taxonomies = $this->get_supported_taxonomies();
		$tax_query            = $query->get( 'tax_query', [] );
		$taxonomies_queried   = array_merge(
			array_column( $tax_query, 'taxonomy' ),
			array_keys( $query->query_vars )
		);
		if ( ! empty( array_intersect( $supported_taxonomies, $taxonomies_queried ) ) ) {
			return true;
		}

Basically, any WP_Query with a taxonomy that is supported by the WooCommerce feature will be integrated. Do we want to change that criteria?

@felipeelia felipeelia added this to the 4.7.0 milestone Jul 7, 2023
@felipeelia felipeelia added needs discussion confirmed bug and removed bug Something isn't working labels Jul 7, 2023
@felipeelia felipeelia mentioned this issue Jul 7, 2023
4 tasks
@anjulahettige
Copy link
Collaborator

We will remove the automatic integration and keep it only for main queries and searches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants