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

Override the default MySQL query limit when counting orders #13

Merged
merged 1 commit into from
Apr 17, 2020

Conversation

stevegrunwell
Copy link
Contributor

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:

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 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 -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/

`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/
@stevegrunwell stevegrunwell added the bug Something isn't working label Apr 17, 2020
@stevegrunwell stevegrunwell requested a review from bswatson April 17, 2020 15:48
@stevegrunwell stevegrunwell added this to the 1.1.2 milestone Apr 17, 2020
@bswatson bswatson merged commit fb9f7b7 into develop Apr 17, 2020
@bswatson bswatson deleted the fix/limiting-on-qualifying-orders branch April 17, 2020 16:27
@stevegrunwell stevegrunwell mentioned this pull request Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants