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

Custom default popularity search breaking Elastic Press Search #3481

Closed
1 task done
alexchudnovsky opened this issue Jun 3, 2023 · 2 comments
Closed
1 task done
Labels

Comments

@alexchudnovsky
Copy link

Describe your question

Hi Everyone,

We have written some custom functions to have a custom sort by popularity search order for woo commerce
Basically where you see the drop down on the catalog listing page, sort by latest, sort by popularity etc

But if we make the default sorting order for woo commerce our 'sort by popularity' option
it completely breaks the elasticPress search

I am wondering if anyone can please let me know what would I need to do, so that we can set up our custom sort by popularity as default without breaking ElasticPress search

Below I have attached the custom code for our sort by popularity search

public function custom_woocommerce_catalog_orderby( $sortby ) {
	$wcgp_settings_options = get_option( 'wcgp_settings_options' );	
	if(isset($wcgp_settings_options['wcgp_settings_enabelgpp']) && $wcgp_settings_options['wcgp_settings_enabelgpp']==1) {
		unset($sortby['popularity']);
		$sortby['grouped_popularity'] = __( 'Sort by popularity', 'woocommerce' );
	}	
	if(isset($wcgp_settings_options['wcgp_settings_enabelsortbya2z']) && $wcgp_settings_options['wcgp_settings_enabelsortbya2z']==1) {
		$sortby['alphabetical'] = __( 'Sort by name: A to Z', 'woocommerce' );
	}		
	return $sortby;
}
function custom_woocommerce_get_catalog_ordering_args( $args ) {

	$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

	if ( 'grouped_popularity' == $orderby_value ) {
		$args['orderby'] = 'meta_value_num';
		$args['order'] = 'DESC';
		$args['meta_key'] = 'total_sales';
		$args['tax_query'] = array(
			array(
				'taxonomy' => 'product_type',
				'field'    => 'slug',
				'terms'    => 'grouped',
			),
		);
	}	
	if ( $orderby_value === 'alphabetical' ) {
		$args['orderby'] = 'title';
		$args['order'] = 'asc';
	}
	return $args;
}

And you can see how this is getting intitialsied

public function __construct() {	
	add_action('admin_menu', array($this, 'wcgp_add_submenu'));

	add_action('admin_init', array($this, 'register_wcgp_settings'));

	add_filter('cron_schedules', array($this,'add_cron_interval_for_ten_mins'));
	add_action('update_grouped_product_sales', array($this,'update_grouped_product_sales'));	
	// Schedule the event to run every 10 minutes
	if (!wp_next_scheduled('update_grouped_product_sales')) {
		wp_schedule_event(time(), 'ten_minutes', 'update_grouped_product_sales');
	}		
	add_filter( 'woocommerce_default_catalog_orderby_options', array($this,'custom_woocommerce_catalog_orderby') );
	add_filter( 'woocommerce_catalog_orderby', array($this,'custom_woocommerce_catalog_orderby') );

	add_filter( 'woocommerce_get_catalog_ordering_args', array($this,'custom_woocommerce_get_catalog_ordering_args') );
	
	add_action('init',array( $this, 'callAFunction' ));// for calling all ajax based frunction from here
	
}

Many thanks in advance

With Kind Regards,
Alex

Code of Conduct

  • I agree to follow this project's Code of Conduct
@burhandodhy
Copy link
Contributor

Hi @alexchudnovsky,

This is a known issue that has already been fixed and will be released in next ElasticPress version.

Regards,
Burhan

@alexchudnovsky
Copy link
Author

Hi Burhan,

Many thanks for your reply, that is good news

Do you have any prediction when the next version will be released ?

Cheers,
Alex

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

No branches or pull requests

2 participants