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

Deprecated WC_Cart->is_coupon_emails_allowed in favour of static DiscountsUtil::is_coupon_emails_allowed #47589

Merged
merged 11 commits into from
May 24, 2024

Conversation

wavvves
Copy link
Contributor

@wavvves wavvves commented May 17, 2024

Submission Review Guidelines:

Changes proposed in this Pull Request:

This PR refactors code and deprecates and moves is_coupon_emails_allowed() from WC_Cart into a new DiscountsUtil class as it is better suited to live there. This enables 3rd party code to use WC_Discounts in Order context, where a cart might not exist (specific use case linked in the mentioned issue below), thus preventing a fatal error from occurring when trying to access the non-existent cart.

Closes #47330 .

How to test the changes in this Pull Request:

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

  1. Create a new coupon named "exclusive", and set email restriction on it
  2. Install the Code Snippets extensions and add the following snippet:
add_action("wp_loaded","test_coupon",20,1);
function test_coupon(){
	WC()->cart = null; // making sure there is no cart
	$order = new WC_Order();
	$order->apply_coupon('exclusive');
}
  1. Refresh any store page and ensure no critical error is displayed
  2. Repeat the steps on an unpatched installation and verify that after adding the snippet a critical error is produced.

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

Fixed a fatal error when programmatically using the WC_Discounts::class in a context where no cart exists.

Comment

@wavvves wavvves added the type: refactor The issue/PR is related to refactoring. label May 17, 2024
@wavvves wavvves self-assigned this May 17, 2024
@wavvves wavvves added the team: Rubik Store API checkout endpoints, Mini-Cart, Cart and Checkout related issues label May 17, 2024
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label May 17, 2024
Copy link
Contributor

github-actions bot commented May 17, 2024

Hi @Konamiman, @opr,

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

@wavvves wavvves requested a review from opr May 17, 2024 16:25
Copy link
Contributor

github-actions bot commented May 17, 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.

@wavvves wavvves marked this pull request as ready for review May 17, 2024 17:07
@woocommercebot woocommercebot requested a review from a team May 17, 2024 17:07
@wavvves wavvves removed the request for review from a team May 17, 2024 17:12
Copy link
Contributor

@Konamiman Konamiman left a comment

Choose a reason for hiding this comment

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

Given that:

  1. We are trying to not add any new methods to classes in includes (everything new should go in src), and
  2. The code in is_coupon_emails_allowed is acting exclusively on the input (it doesn't depend on any state)

then I think the is_coupon_emails_allowed method should be a static method in a new CouponUtil class (or perhaps DiscountUtil) in src/Utilities (normally I would suggest src/Internal/Utilities instead, but the method is already public anyway).

Also this is a case where I would add unit tests. I can help with that if you want.

@wavvves
Copy link
Contributor Author

wavvves commented May 20, 2024

Given that:

  1. We are trying to not add any new methods to classes in includes (everything new should go in src), and
  2. The code in is_coupon_emails_allowed is acting exclusively on the input (it doesn't depend on any state)

then I think the is_coupon_emails_allowed method should be a static method in a new CouponUtil class (or perhaps DiscountUtil) in src/Utilities (normally I would suggest src/Internal/Utilities instead, but the method is already public anyway).

Also this is a case where I would add unit tests. I can help with that if you want.

I 100% agree with going the static route. I can make the change, but I do have a question. Do you think it is worth considering if, at this point, anything else should qualify for moving into a DiscountsUtil class and move it, or should I just highlight it for a future refactor?
I'm not a big fan of segmented Utility classes, as that doesn't ever sound too "clean cody" to me (maybe the object scope should be reconsidered if anything kinda falls into its responsibilities but it doesn't fit like a glove). So it becomes harder to justify opening a new Util class with just one method, that might never fit anything else in there becoming a "one trick dog" 🙄

@barryhughes
Copy link
Member

Adding a couple of thoughts:

Should be a static method in a new CouponUtil class
...
[Should] anything else should qualify for moving into a DiscountsUtil class

Small thing, but if a new util class is the way we go here then, in relation to the proposed classname I also prefer Discounts over Coupons in this particular case.

Or should I just highlight it for a future refactor?

I think this is the way to do it. I understand and agree with the concerns around segmentation, but even superficially simple refactorings tend to increase risk and so minimizing the surface area of a change has its own value (in terms of release stability).

@wavvves
Copy link
Contributor Author

wavvves commented May 22, 2024

@Konamiman @barryhughes I pushed some changes:

  • Created a DiscountsUtil class w/ the static method as suggested e26ca24
  • Moved tests that already existed 16d3bd1
  • Took a step further, updating all calls in our codebase for the deprecated method and added a doing it wrong warning to the wrapper. I can revert this if needed 37d95a9

@wavvves wavvves changed the title Deprecated WC_Cart->is_coupon_emails_allowed in favour of WC_Discounts->is_coupon_emails_allowed Deprecated WC_Cart->is_coupon_emails_allowed in favour of static DiscountsUtil::is_coupon_emails_allowed May 22, 2024
@wavvves wavvves requested a review from Konamiman May 22, 2024 12:36
@wavvves wavvves requested a review from barryhughes May 22, 2024 16:50
@wavvves wavvves added the priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. label May 23, 2024
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!

🗣️ Left one note about a possible i18n improvement.

🛒 Tested in the cart and checkout, using coupons with 1 allowed email, 2 allowed emails and no allowed emails, and different combinations of user data.

💻 Tested in the WP shell, with WC()->cart nullified, as a compliment to the provided testing instructions.

🛠️ Additional tests look great!

plugins/woocommerce/includes/class-wc-cart.php Outdated Show resolved Hide resolved
Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com>
@barryhughes
Copy link
Member

Thanks! Good to merge on my end, unless we want either of the other reviewers to approve first?

@wavvves wavvves requested review from Konamiman and removed request for Konamiman May 24, 2024 09:31
@wavvves wavvves dismissed Konamiman’s stale review May 24, 2024 09:40

The requested changes were included.

@wavvves wavvves merged commit 86bc377 into trunk May 24, 2024
26 checks passed
@wavvves wavvves deleted the 47330-removing-cart-dependency-from-coupon-validation branch May 24, 2024 09:40
@github-actions github-actions bot added this to the 9.1.0 milestone May 24, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label May 24, 2024
@rodelgc rodelgc modified the milestones: 9.1.0, 9.0.0 May 28, 2024
github-actions bot pushed a commit that referenced this pull request May 28, 2024
…ountsUtil::is_coupon_emails_allowed (#47589)

* Deprecated WC_Cart->is_coupon_emails_allowed in favour of WC_Discounts->is_coupon_emails_allowed

* Add changefile(s) from automation for the following project(s): woocommerce

* Created new DiscountsUtil class and moved is_coupon_emails_allowed()

* Refactored tests.

* Updated internal WC_Cart::is_coupon_emails_allowed calls and added a doing it wrong warning for 3rd party tracking.

* Linting.

* Refactored and added new asserts to DiscountsUtilTest.

* Linting.

* Linting and updated test.

* Added translation to wc_doing_it_wrong() as sugested.

Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com>

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com>
rodelgc pushed a commit that referenced this pull request May 28, 2024
* Deprecated WC_Cart->is_coupon_emails_allowed in favour of static DiscountsUtil::is_coupon_emails_allowed (#47589)

* Deprecated WC_Cart->is_coupon_emails_allowed in favour of WC_Discounts->is_coupon_emails_allowed

* Add changefile(s) from automation for the following project(s): woocommerce

* Created new DiscountsUtil class and moved is_coupon_emails_allowed()

* Refactored tests.

* Updated internal WC_Cart::is_coupon_emails_allowed calls and added a doing it wrong warning for 3rd party tracking.

* Linting.

* Refactored and added new asserts to DiscountsUtilTest.

* Linting.

* Linting and updated test.

* Added translation to wc_doing_it_wrong() as sugested.

Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com>

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com>

* Prep for cherry pick 47589

---------

Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com>
Co-authored-by: WooCommerce Bot <no-reply@woocommerce.com>
@rodelgc rodelgc 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 May 28, 2024
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. priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. status: analysis complete Indicates if a PR has been analysed by Solaris team: Rubik Store API checkout endpoints, Mini-Cart, Cart and Checkout related issues type: refactor The issue/PR is related to refactoring.
Projects
None yet
4 participants