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 simple products to the Analytics orders query for the attributes filter #44901

Merged
merged 7 commits into from Mar 7, 2024

Conversation

louwie17
Copy link
Contributor

@louwie17 louwie17 commented Feb 22, 2024

Submission Review Guidelines:

Changes proposed in this Pull Request:

Modifies the attributes filter to include simple products that use global attributes.

Closes #32237

How to test the changes in this Pull Request:

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

  1. Create two global attributes, like: size with terms: small, medium, large and colour with terms: red, yellow, blue.
  2. Create two products one simple product and one variable
  • The simple can contain one attribute of each ( small and red? ), can name it a "small red t-shirt"
  • The variable can include all options of both attributes, can name it "Variable t-shirts"
  1. Buy the simple product in an order by it-self
  2. Buy the variable product with the small attribute selected and any colour.
  3. Mark the order as completed
  4. Go to WooCommerce > Status > Scheduled actions and make sure none are pending.
  5. Go to Reports > Orders
  6. Change the date to from beginning of the year to today
  7. Filter by advanced search
  8. Choose attribute from the list
  9. Set to "is", and choose Size and pick attribute small
  10. Click 'filter'
  11. There should be two orders present, one with the simple product and one with the variable
  12. If you selected a different colour for the variable order then the simple product then add another attribute filter and choose Color
  13. It should show the correct data.

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 Feb 22, 2024
Copy link
Contributor

github-actions bot commented Feb 22, 2024

Test Results Summary

Commit SHA: 6147225

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 38s
E2E Tests327002403517m 39s

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.

@louwie17 louwie17 force-pushed the fix/32237_orders_simple_products branch from 3756836 to ec04e6e Compare February 23, 2024 14:31
$attr_term = get_term_by( 'slug', $meta_value, $meta_key );
if ( false !== $attr_term ) {
$term_id = $attr_term->term_id;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

$term_id wasn't previously being used, but now it is, this makes sure it is set in both instances.

$sql_clauses['where'][] = $wpdb->prepare(
"
( ( {$join_alias}.meta_key = %s AND {$join_alias}.meta_value {$comparator} %s ) or (
{$wpdb->prefix}wc_order_product_lookup.variation_id = 0 and {$wpdb->prefix}wc_order_product_lookup.product_id {$in_comparator} ( select product_id from {$wpdb->prefix}wc_product_attributes_lookup where is_variation_attribute=0 and term_id = %s )
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added an order sub query, to see if a simple product has a matching attribute id.
Note: this query works only for simple products with global attributes, given the attributes filter only shows global attributes to filter by.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not a fan of this line. Maybe extracting the subquery to a new variable would improve its legibility.

Maybe something like this would work:

$prepared_term_id = intval( $term_id );
$subquery = "SELECT product_id FROM {$wpdb->prefix}wc_product_attributes_lookup WHERE is_variation_attribute = 0 AND term_id = {$prepared_term_id}";
$where_condition = "
	( ( {$join_alias}.meta_key = %s AND {$join_alias}.meta_value {$comparator} %s ) OR
		({$wpdb->prefix}wc_order_product_lookup.variation_id = 0 AND
		{$wpdb->prefix}wc_order_product_lookup.product_id {$in_comparator} ({$subquery}) ) )";
$sql_clauses['where'][] = $wpdb->prepare( $where_condition, $meta_key, $meta_value );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed as part of: 96f2b45

@@ -207,8 +229,8 @@ public function test_product_attributes_filter() {
$response_orders = $response->get_data();

$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, $response_orders['totals']['orders_count'] );
$this->assertEquals( 11, $response_orders['totals']['orders_count'] );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is 11 now, because previously even when using is_not it would filter out any orders with simple products. Now the query actually works correctly for is_not.

@louwie17 louwie17 marked this pull request as ready for review February 23, 2024 14:38
@louwie17 louwie17 requested a review from a team February 23, 2024 14:38
Copy link
Contributor

github-actions bot commented Feb 23, 2024

Hi @octaedro,

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

@louwie17 louwie17 force-pushed the fix/32237_orders_simple_products branch from ec04e6e to 287f438 Compare March 4, 2024 21:20
@octaedro octaedro requested review from octaedro and removed request for a team March 6, 2024 18:35
octaedro
octaedro previously approved these changes Mar 6, 2024
Copy link
Contributor

@octaedro octaedro left a comment

Choose a reason for hiding this comment

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

Good job @louwie17! The code looks good and is testing well on my end. I left a small suggestion, although the PR is ok as it is now, so I'll approve it.

@louwie17 louwie17 force-pushed the fix/32237_orders_simple_products branch from 287f438 to 96f2b45 Compare March 6, 2024 21:34
@louwie17
Copy link
Contributor Author

louwie17 commented Mar 6, 2024

@octaedro could you have another look and also test it once more ( I don't have time ) and if good merge it for me?
I just addressed your feedback.

@louwie17 louwie17 requested a review from octaedro March 6, 2024 21:37
@louwie17 louwie17 dismissed octaedro’s stale review March 6, 2024 21:37

As I just submitted new changes

@louwie17 louwie17 enabled auto-merge (squash) March 6, 2024 21:38
Copy link
Contributor

@octaedro octaedro left a comment

Choose a reason for hiding this comment

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

LGTM! 🚀

@louwie17 louwie17 merged commit d6cbbfb into trunk Mar 7, 2024
33 checks passed
@louwie17 louwie17 deleted the fix/32237_orders_simple_products branch March 7, 2024 13:52
@github-actions github-actions bot added this to the 8.8.0 milestone Mar 7, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Mar 7, 2024
@alopezari alopezari 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 Mar 11, 2024
Konamiman pushed a commit that referenced this pull request Mar 13, 2024
…filter (#44901)

* Add subquery for simple products

* Update query to make use of product lookup instead

* Add changelog

* Revert change

* Update PHP tests and fix lint errors

* Move subquery out to its own variable to make it more readable
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 team: Mothra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Analytics: Orders report does not filter by attributes on simple products
3 participants