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

Trigger doing_it_wrong for CPT order queries with HPOS query args #47457

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

jorgeatorres
Copy link
Member

@jorgeatorres jorgeatorres commented May 14, 2024

Submission Review Guidelines:

Changes proposed in this Pull Request:

With HPOS we introduced a few new capabilities in order querying. In particular, when HPOS is the backing datastore for orders, you can create complex queries using meta_query and field_query.

We have explored adding a compat layer that would allow some of those queries to work with the CPT datastore, but due to the differences in the underlying db structure this is not always possible, and might not be worth the effort given we want to push HPOS over CPT.

This PR introduces a "doing it wrong" warning when either meta_query or field_query are being used in an order query and the datastore is the CPT one. Currently, those special arguments are silently ignored, producing a result set that can be confusing.

⚠️ Given a bunch of filters are involved in any query, it could happen that 3rd party code adds support for, say, meta_query to the CPT datastore, and yet we would be triggering a warning. Not sure how likely that is and the code does limit the warning to instances of the default CPT datastore (so if extended, it won't trigger any warnings), but I guess it could still happen. Could this be a problem?

Closes #46076.

How to test the changes in this Pull Request:

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

  1. Make sure WordPress posts storage is selected in WC > Settings > Advanced > Features > Order data storage.
    If necessary, run wp hpos sync to sync so that the option becomes available.

  2. Open a CLI shell (wp shell) and confirm that you get this "doing it wrong" notice...

    Notice: Function WC_Order_Data_Store_CPT::query was called incorrectly. Order query argument (field_query) is not supported on non-HPOS datastores.

    ... when running any of the following commands:

    wc_get_orders( array( 'field_query' => array( 'operator' => 'AND'  ), 'meta_query' => array(), 'return' => 'ids' ) );
    
    wc_get_orders( array( 'field_query' => array(), 'meta_query' => array( 'operator' => 'AND' ), 'return' => 'ids' ) );
    
    wc_get_orders( array( 'field_query' => array( 'operator' => 'OR' ), 'meta_query' => array( 'operator' => 'AND' ), 'return' => 'ids' ) );
    
  3. Drop the following snippet somewhere suitable (like wp-content/mu-plugins/) to "simulate" the default CPT order datastore not being used but extended in 3rd party code:

    <?php
    add_filter(
        'woocommerce_order_data_store',
        function ( $ds ) {
     	   static $c;
    
     	   if ( ! class_exists( 'MyOwnDatastore' ) ) {
     		   class MyOwnDatastore extends WC_Order_Data_Store_CPT {
     		   }
    
     		   $c = new MyOwnDatastore;
     	   }
    
     	   return $c;
        }
    );
  4. Confirm that no notice is triggered when you run the commands from step 2.

  5. Remove the PHP snippet from step 3.

  6. Change the order data storage back to HPOS in WC > Settings > Advanced > Features.

  7. Confirm that no notice is triggered when you run the commands from step 2.

Changelog entry

  • Automatically create a changelog entry from the details below.

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Comment

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label May 14, 2024
Copy link
Contributor

github-actions bot commented May 14, 2024

Test using WordPress Playground

The changes in this pull request can be previewed and tested using a WordPress Playground instance.
WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Test this pull request with WordPress Playground.

Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit.

@jorgeatorres jorgeatorres changed the title Trigger doint_it_wrong for CPT order queries with HPOS query args Trigger doing_it_wrong for CPT order queries with HPOS query args May 28, 2024
@jorgeatorres jorgeatorres marked this pull request as ready for review May 29, 2024 21:02
@jorgeatorres jorgeatorres requested review from a team and naman03malhotra and removed request for a team May 29, 2024 21:02
Copy link
Contributor

Hi @naman03malhotra,

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

1 similar comment
Copy link
Contributor

Hi @naman03malhotra,

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

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

Successfully merging this pull request may close these issues.

wc_get_orders() unexpected behavior with legacy orders, field_query and meta_query ignored
1 participant