Change couponName
filter to coupons
and update documentation
#4312
Conversation
couponName
filter to coupons
and update documentation
Size Change: +3 kB (0%) Total Size: 997 kB
ℹ️ View Unchanged
|
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.
This looks good, I really like the new field idea. I also recommend mentioning in the changelog the bug this PR is fixing from a user's perspective. Nice work 👍
if you dynamically generate a coupon code that is not user friendly. It may, therefore, be desirable to change the way | ||
this code is displayed. To do this, the filter `couponName` exists. | ||
if you dynamically generate a coupon code that is not user-friendly. It may, therefore, be desirable to change the way | ||
this code is displayed. To do this, the filter `couponName` exists. |
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.
I think you missed one replace here: the filter coupons
exists instead of couponName
|
||
The additional argument supplied to this filter is: `{ context: 'summary', coupon: CartCoupon }`. A `CartCoupon` has the following shape: | ||
The additional argument supplied to this filter is: `{ context: 'summary' }`. A `CartCoupon` has the following shape: |
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.
I am wondering if coupon was actually the only additional argument and maybe this line needs to go away?
About CartCoupon
, as there is no longer any reference to it in this paragraph, I would remove the sentence from here and make it standalone.
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.
Unrelated to the changes, I've looked at the readme and there is no example of how one should actually use this filter. It would be great if you could add one, two examples with the coupons filter.
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.
This isn't the case, the context is passed as the only additional argument, the coupon is no longer passed. The current state of this file does correctly describe what is passed to the filter function.
// functionality when it comes to removing the coupon. | ||
const cartCoupons = cartData.coupons.map( ( coupon ) => ( { | ||
...coupon, | ||
text: coupon.code, |
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.
Wondering if label
would be more appropriate than text
. For example, we have the Total label that the user can change, so to keep it in the same registry I'd say a coupon label
makes it more clear that we are talking about what text is displayed. What do you think?
This is needed to allow extensions to change the text without affecting the code.
@@ -92,7 +89,7 @@ const TotalsDiscount = ( { | |||
'Remove coupon "%s"', | |||
'woo-gutenberg-products-block' | |||
), | |||
filteredCouponCode | |||
cartCoupon.text |
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.
Shouldn't this be cartCoupon.label
as well?
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.
Great spot! Updated
} | ||
return { | ||
...coupon, | ||
text: sprintf( |
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.
You need to replace text with label here as well.
|
||
| Before | After | | ||
|---|---| | ||
| <img src="https://user-images.githubusercontent.com/5656702/123768988-bc55eb80-d8c0-11eb-9262-5d629837706d.png" /> | * | |
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.
There is no after photo here from what I see.
@@ -201,6 +206,39 @@ __experimentalRegisterCheckoutFilters( 'my-hypothetical-price-plugin', { | |||
|---|---| | |||
| <img src="https://user-images.githubusercontent.com/5656702/117035086-d5488300-acfb-11eb-9954-feb326916168.png" width=400 /> | <img src="https://user-images.githubusercontent.com/5656702/117035616-70415d00-acfc-11eb-98d3-6c8096817e5b.png" width=400 /> | | |||
|
|||
### Change the name of a coupon |
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.
Great job adding this example, I'm sure it's gonna be super useful
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.
Nice work!
Description
There is a bug where the coupon filter doesn't work if more than one coupon is added to the cart. This PR will change the way the coupon filter works to ensure the filter is only invoked once per render. Unfortunately this is a breaking change.
Another way to solve this would be to implement our own memoization function, and use that in
__experimentalApplyCheckoutFilter
rather thanReact.useMemo
, or to simply remove memoization altogether (not desirable).Changes made in this PR
text
property to eachcoupon
in the cart. This is required because when extensions change how the coupon is displayed, we need to keep a record of the code, so we can remove it from the cart if the shopper desires.couponNames
filter, and replace it withcoupons
. Move the filter outside of the loop so it is always called once per render.couponNames
in its filters object.Fixes #4311
How to test the changes in this Pull Request:
testcoupon
.packages/checkout/registry/index.ts
)couponName is deprecated. Please use coupons instead.
testcoupon
is now shown asthis is custom coupon text!
Changelog