-
Notifications
You must be signed in to change notification settings - Fork 314
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
QUESTION: woocommerce product search results sort by stock_status #2473
Comments
Just another note. I found that the Woocommerce Feature in ElasticPress is overriding the orderby value in the If I comment the following lines: if ( ! empty( $s ) ) {
// Comment this to avoid overriding orderby stock_status
// $query->set( 'orderby', false ); // Just order by relevance.
/**
* Default order when doing search in Woocommerce is 'ASC'
* These lines will change it to 'DESC' as we want to most relevant result
*/
if ( empty( $_GET['orderby'] ) && $query->is_main_query() ) { // phpcs:ignore WordPress.Security.NonceVerification
// Comment this to avoid outofstock showing before instock
// $query->set( 'order', 'DESC' );
} Then the search results come ordered as intended (ie, in stock products come before out of stock). But I can see that by doing the ASC order I might be messing with the relevancy of the result. Can anyone suggest what is the best way to achieve this? |
Hi @beazuadmin, First of all, sorry for the late reply here. The lines you commented out don't seem to be needed anymore, as I outlined in #2577. With those commented out, you can use this code to have the order by stock AND relevance applied:
That PR will go through our code review process and will probably get merged for our next release. Until then, for you and any other users facing the same issue, this snippet should be enough:
Thanks! |
As #2577 was merged, I'm closing out the issue. |
Hi @felipeelia just to confirm that the temporary solution you outline above works with the current version of the plugin in production. Thanks! |
Hello @felipeelia The code worked. Now the out-of-stock products are showing at the end of my listing. but when I choose the default order status as “price – low to high”, it does not work. Would you know any workaround to make it work? Thank you. |
Hello @felipeelia Is there any update on the above issue? Thanks |
Hi @Ahammed-Navas, Although I'm leaving an alternative version of the code below, I'd like to set some expectations, so you don't have to wait for our replies.
Thanks!
|
Hello @felipeelia Thanks for your reply. I tried adding the code above you shared and unfortunately, it did not work as expected. Sorting now works correctly. However, sorting out-of-stock products last in the archive page is not working. Can you please provide us with the correct code? We need to resolve the issue as soon as possible. Thank you |
Hi @Ahammed-Navas my colleague @felipeelia has very nicely explained to you that this work is custom development well outside the scope of our free, open source plugin support that we provide here for bug fixes and feature requests. If you would like to someone else to write code for you, we can provide custom consulting on a paid basis. You may also prefer to use Codeable which has several ElasticPress experts available: https://www.codeable.io/developers/?developer_search=elasticpress |
Hi @brandwaffle ,
Therefore, the issue is due to ElasticPress. That's why I asked you about it. Would you mind checking the code again? I hope we can resolve it. Thanks |
@Ahammed-Navas it works without ElasticPress because you have written a direct SQL query and as we've explained to you before, ElasticPress integrates with WP_Query. ElasticPress doesn't support 100% of all themes and plugins in the WordPress ecosystem, and it certainly can't support custom code that circumvents the WP Query API, because that's impossible. I'm sorry you're not able to make it work for your purposes, but we do not provide custom development support here, as I've stated numerous times. If you have legitimate issues to share on this repo, you are welcome to do so. However, if you continue to request individualized support for your specific integration, we may have to take additional action up to and including blocking your ability to submit tickets to our repo. |
Is there a way to use _stock_status metadata in a woocommerce product to sort search results such that it shows all out-of-stock products last?
When ElasticPress is not active, the following filter works correctly:
But ElaticPress seems to ignore that.
If I step on the function get_es_posts in includes/classes/Indexable/Post/QueryIntegration.php I can see that the $query is setup correctly, and it has the right _stock_status meta_query set. However, it seems that $query->query_vars has 'orderby' set to false and elastic press never picks up the _stock_status meta query from the query.
Any idea how to make this work?
Thanks
The text was updated successfully, but these errors were encountered: