Override the default MySQL query limit when counting orders#13
Merged
Override the default MySQL query limit when counting orders#13
Conversation
`OrderLimiter::count_qualifying_orders()` relies on `wc_get_orders()`, which is several layers of abstraction over `WP_Query`.
Currently, if we analyze the query, it looks something like this:
```sql
SELECT wptests_posts.ID
FROM wptests_posts
WHERE 1=1
AND ( wptests_posts.post_date_gmt >= '2020-04-17 00:00:00' )
AND wptests_posts.post_type = 'shop_order'
AND (
(
wptests_posts.post_status = 'wc-pending'
OR wptests_posts.post_status = 'wc-processing'
OR wptests_posts.post_status = 'wc-on-hold'
OR wptests_posts.post_status = 'wc-completed'
OR wptests_posts.post_status = 'wc-cancelled'
OR wptests_posts.post_status = 'wc-refunded'
OR wptests_posts.post_status = 'wc-failed'
)
)
ORDER BY wptests_posts.post_date DESC LIMIT 0, 10
```
Unfortunately, that last line is the problem: we're limiting the qualifying orders to 10, which is the default LIMIT value for `WP_Query`.
This commit overrides that default, setting it to either the limit set by the store _or_ `1000` (whichever is higher); this ensures that we're not running a `-1` limit on a store that could potentially have a very large threshold for orders (e.g. "oh, we only want to accept 1M orders/week").
Eventually, it may be worth rewriting this function to use an explicit SQL `COUNT(*)` query (thus eliminating limit concerns), but we want to ensure we do that in a way that maintains compatibility with WooCommerce's CRUD APIs.
This should also resolve the two open support tickets on WordPress.org:
1. https://wordpress.org/support/topic/limit-orders-did-not-work/
2. https://wordpress.org/support/topic/orders-have-exceeded-the-limit/
bswatson
approved these changes
Apr 17, 2020
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
OrderLimiter::count_qualifying_orders()relies onwc_get_orders(), which is several layers of abstraction overWP_Query.Currently, if we analyze the query, it looks something like this:
Unfortunately, that last line is the problem: we're limiting the qualifying orders to 10, which is the default LIMIT value for
WP_Query.This PR overrides that default, setting it to either the limit set by the store or
1000(whichever is higher); this ensures that we're not running a-1limit on a store that could potentially have a very large threshold for orders (e.g. "oh, we only want to accept 1M orders/week").Eventually, it may be worth rewriting this function to use an explicit SQL
COUNT(*)query (thus eliminating limit concerns), but we want to ensure we do that in a way that maintains compatibility with WooCommerce's CRUD APIs.This should also resolve the two open support tickets on WordPress.org: