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

WooCommerce Feature - whitelist_meta_keys and orderly #2546

Closed
seldimi opened this issue Jan 11, 2022 · 4 comments
Closed

WooCommerce Feature - whitelist_meta_keys and orderly #2546

seldimi opened this issue Jan 11, 2022 · 4 comments
Assignees
Labels
enhancement module:woocommerce Issues related to the WooCommerce functionality
Milestone

Comments

@seldimi
Copy link

seldimi commented Jan 11, 2022

Hello.
I have recently started digging into Elasticpress code. I have faced two issues related to WooCommerce that made me edit WooCommerce.php feature.

First of all, WooCommerce allows users to create their own ordering rules with any custom field. With woocommerce_default_catalog_orderby_options and woocommerce_get_catalog_ordering_args, you can define your own ordering. For instance, let's say I want to make a sorting by discount percentage, which is saved at _discount_label field.

I had to modify the array on whitelist_meta_keys() in order to add my field.
Possible and easy solution: Adding an filter (apply_filters) so a user can add/remove fields on that array.

Second, I had to modify the translate_args at the orderby section ($_GET['orderby']) in order to add my custom ordering.

case 'discount':
    $query->set( 'orderby', $this->get_orderby_meta_mapping( '_discount_label' ) );
    $query->set( 'order', 'DESC' );
    break;

Then I've used the orderby_meta_mapping filter that you have, in order to set my _discount_label mapping into args.

Solution: Allow the editing of the switch of( $_GET['orderby'] )

@brandwaffle
Copy link
Contributor

@seldimi as a workaround, you can always whitelist fields via the same filter that the WooCommerce Feature uses: ep_prepare_meta_allowed_protected_keys. That way you won't need to edit plugin code. That said, I like the idea of making the WooCommerce meta keys themselves filterable, as that makes a lot more sense from a code organization/logic standpoint. We'll discuss at our next meeting and get this slotted for development.

@seldimi
Copy link
Author

seldimi commented Jan 12, 2022

Great. Another feedback I could give is the double or tripe order by.
If you check $query->set('orderby') can take an array for both ASC or DESC parameters. Eg order by date DESC, discount ASC.

It would be good if that could be added

Keep up :)

@felipeelia
Copy link
Member

Hi @seldimi, thanks for all the feedback!

ElasticPress already supports multiple orderby parameters out-of-the-box. I left in this comment an example on how to do it but the gist of it is to call something like:

$query->set(
	'orderby',
	[
		'meta._stock_status.raw' => 'ASC',
		'relevance'              => 'DESC',
	]
);

Is this something you already tried and failed? Thanks!

@felipeelia felipeelia added the module:woocommerce Issues related to the WooCommerce functionality label Mar 31, 2022
@felipeelia felipeelia added this to the 4.7.0 milestone Jun 16, 2023
@felipeelia felipeelia self-assigned this Jul 19, 2023
@felipeelia
Copy link
Member

Closing this one out as working with orderby clauses should be much easier after #3512 and #3469.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement module:woocommerce Issues related to the WooCommerce functionality
Projects
None yet
Development

No branches or pull requests

3 participants