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

Add a filter to OrdersTableQuery to allow overriding of HPOS queries #39945

Merged
merged 9 commits into from
Sep 12, 2023

Conversation

lsinger
Copy link
Contributor

@lsinger lsinger commented Aug 29, 2023

Submission Review Guidelines:

Changes proposed in this Pull Request:

This PR adds a filter to the OrdersTableQuery class to allow for the overriding of HPOS queries.

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

  1. Create four orders and note their IDs.
  2. Add a filter snippet to, for example, woocommerce.php, so that an HPOS-based order query will only return those IDs. Example:
$callback = function( $result, $query_object, $sql ) {
	// an array of the order IDs to return, the number of orders, and the number of pages
	// for this test, simply replace the two order IDs with the ones you noted down
	return array( array( 15, 18 ), 2, 1 );
};
add_filter( 'woocommerce_hpos_pre_query', $callback, 10, 3 );
  1. Go to the order list screen and verify that all four orders are displayed.
  2. Go to Settings / Features and activate HPOS
  3. Go to the order list screen and verify that only the two orders returned by the callback are displayed.

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Aug 29, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 29, 2023

Test Results Summary

Commit SHA: d8abb70

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 58s
E2E Tests1950015021017m 59s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

@lsinger lsinger self-assigned this Aug 31, 2023
@lsinger lsinger requested review from vedanshujain and a team August 31, 2023 12:16
@github-actions
Copy link
Contributor

github-actions bot commented Aug 31, 2023

Hi @vedanshujain,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

@lsinger lsinger marked this pull request as ready for review August 31, 2023 12:17
@lsinger lsinger requested review from a team and removed request for a team August 31, 2023 12:17
Copy link
Contributor

@rrennick rrennick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lsinger Nice work. I noted a few things and will leave the approval for @vedanshujain .

Copy link
Contributor

@vedanshujain vedanshujain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, added few more comments along with what Ron suggested.

Copy link
Member

@barryhughes barryhughes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of (very) minor notes about the filter docblock. Beyond that, I'm curious if we should provide a mechanism for query overrides to share the SQL they use? To illustrate:

use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableQuery;

add_filter( 'woocommerce_hpos_pre_query', function ( $override ) {
	// Assume I do some custom SQL here, before returning an alternative data-set.
	return [
		'orders' => [ /* some orders */ ],
	];
} );

$query = new OrdersTableQuery;

// I get back the default SQL, not the SQL used by the callback.
echo $query->request;

This probably isn't essential (there is no @property-read declaration for request), but I thought I'd ask the question.

@lsinger
Copy link
Contributor Author

lsinger commented Sep 11, 2023

I'm curious if we should provide a mechanism for query overrides to share the SQL they use?

@barryhughes we could add that as an optional field in the $order_data array. Did you have a specific use case in mind? I'd like to make sure we understand what kind of usage we want to support before adding this.

Since it would be an additional optional return value, I also wonder whether this should we a separate discussion and PR?

@barryhughes
Copy link
Member

Did you have a specific use case in mind?

No, not really—I just noticed that we can retrieve the query SQL (ie, via $query->request) and realized it would be incorrect in these situations, and therefore might be confusing for anyone who is using this property to do some debugging.

On taking a second look, I don't think we use the magic request property ourselves and, since we're inside the internal namespace, third parties probably shouldn't be using it anyway: let's move on, we can ignore this.

Copy link
Member

@barryhughes barryhughes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, not sure if we want final sign-off by one of the other reviewers. Otherwise, may be a good candidate for needs: analysis.

@lsinger
Copy link
Contributor Author

lsinger commented Sep 11, 2023

final sign-off by one of the other reviewers

I re-requested another review from @vedanshujain as the HPOS DRI just for an extra level of thoroughness.

Copy link
Contributor

@vedanshujain vedanshujain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, added couple of super minor comments. Another note is, we require more detailed testing instructions than provided in the PR, can you please update them with an example of the filter than can be added to snippets? I think a nice testing instruction set could go like:

  1. Create couple of orders, note their ID.
  2. Add a filter snippet so that order query only return those IDs.
  3. Go to order list screen, and verify that only those orders are displayed.

lsinger and others added 2 commits September 12, 2023 16:45
Co-authored-by: Vedanshu Jain <vedanshu.jain.2012@gmail.com>
@lsinger lsinger merged commit 4a45c95 into trunk Sep 12, 2023
27 checks passed
@lsinger lsinger deleted the add/filter-hpos-query-override branch September 12, 2023 16:18
@github-actions github-actions bot added this to the 8.2.0 milestone Sep 12, 2023
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Sep 12, 2023
@lanej0 lanej0 added needs: internal testing Indicates if the PR requires further testing conducted by Solaris status: analysis complete Indicates if a PR has been analysed by Solaris and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: internal testing Indicates if the PR requires further testing conducted by Solaris plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants