-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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
Deprecated WC_Cart->is_coupon_emails_allowed in favour of static DiscountsUtil::is_coupon_emails_allowed #47589
Conversation
…s->is_coupon_emails_allowed
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: |
Test using WordPress PlaygroundThe changes in this pull request can be previewed and tested using a WordPress Playground instance. 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that:
- We are trying to not add any new methods to classes in
includes
(everything new should go insrc
), and - 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? |
Adding a couple of thoughts:
Small thing, but if a new util class is the way we go here then, in relation to the proposed classname I also prefer
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). |
…doing it wrong warning for 3rd party tracking.
@Konamiman @barryhughes I pushed some changes: |
plugins/woocommerce/tests/legacy/unit-tests/discounts/discounts-util.php
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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!
Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com>
Thanks! Good to merge on my end, unless we want either of the other reviewers to approve first? |
…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>
* 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>
Submission Review Guidelines:
Changes proposed in this Pull Request:
This PR refactors code and deprecates and moves
is_coupon_emails_allowed()
fromWC_Cart
into a newDiscountsUtil
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:
Changelog entry
Significance
Type
Message
Fixed a fatal error when programmatically using the WC_Discounts::class in a context where no cart exists.
Comment