Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Change couponName filter to coupons and update documentation #4312

Merged
merged 11 commits into from Jul 1, 2021

Conversation

opr
Copy link
Contributor

@opr opr commented Jun 5, 2021

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 than React.useMemo, or to simply remove memoization altogether (not desirable).


Changes made in this PR

  • Add a text property to each coupon 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.
  • Remove the couponNames filter, and replace it with coupons. Move the filter outside of the loop so it is always called once per render.
  • Add deprecation notice if an extension has included couponNames in its filters object.
  • Add documentation about new filter.

Fixes #4311

How to test the changes in this Pull Request:

  1. Ensure two coupons are available on your store. Name one of them testcoupon.
  2. Go to the Cart block and add them both.
  3. Ensure they work, and can be removed.
  4. Try with three+ coupons.
  5. Add this code somewhere in your application (I added it to the bottom of packages/checkout/registry/index.ts)
__experimentalRegisterCheckoutFilters( 'blocks', {
	couponName: ( value ) => {
		return value;
	},
} );
  1. Ensure the deprecation warning is shown in the console. couponName is deprecated. Please use coupons instead.
  2. Remove that code and add:
__experimentalRegisterCheckoutFilters( 'blocks', {
	coupons: ( value ) => {
		return value.map( ( coupon ) => {
			if ( coupon.code !== 'testcoupon' ) {
				return coupon;
			}
			return {
				...coupon,
				text: 'this is custom coupon text!',
			};
		} );
	},
} );
  1. Reload the page and ensure the coupon that was previously labelled testcoupon is now shown as this is custom coupon text!

Changelog

Remove couponName filter and replace it with coupons filter.

@opr opr added type: bug The issue/PR concerns a confirmed bug. category: extensibility Work involving adding or updating extensibility. Useful to combine with other scopes impacted. focus: blocks Specific work involving or impacting how blocks behave. block: cart Issues related to the cart block. block: checkout Issues related to the checkout block. labels Jun 5, 2021
@opr opr requested a review from a team as a code owner June 5, 2021 07:03
@opr opr self-assigned this Jun 5, 2021
@opr opr requested review from ralucaStan and removed request for a team June 5, 2021 07:03
@opr opr changed the title Fix coupon filters and add snackbar filter Change couponName filter to coupons and update documentation Jun 5, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Jun 5, 2021

Size Change: +3 kB (0%)

Total Size: 997 kB

Filename Size Change
build/active-filters-frontend.js 8.13 kB +89 B (+1%)
build/active-filters.js 7.68 kB +158 B (+2%)
build/all-products-frontend.js 35.1 kB +213 B (+1%)
build/all-products.js 37.3 kB +615 B (+2%)
build/all-reviews.js 9.4 kB +56 B (+1%)
build/atomic-block-components/add-to-cart--atomic-block-components/button--atomic-block-components/image---a7e2bb9b.js 2.56 kB +53 B (+2%)
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 2 kB -2 B (0%)
build/atomic-block-components/add-to-cart--atomic-block-components/image--atomic-block-components/title.js 334 B -1 B (0%)
build/atomic-block-components/add-to-cart-frontend.js 8.48 kB -196 B (-2%)
build/atomic-block-components/add-to-cart.js 7.53 kB -212 B (-3%)
build/atomic-block-components/button-frontend.js 1.76 kB +35 B (+2%)
build/atomic-block-components/button.js 876 B +33 B (+4%)
build/atomic-block-components/category-list-frontend.js 469 B -1 B (0%)
build/atomic-block-components/category-list.js 476 B -2 B (0%)
build/atomic-block-components/image-frontend.js 1.69 kB +37 B (+2%)
build/atomic-block-components/image.js 1.35 kB +39 B (+3%)
build/atomic-block-components/price-frontend.js 2.08 kB +32 B (+2%)
build/atomic-block-components/price.js 2.1 kB +31 B (+1%)
build/atomic-block-components/rating-frontend.js 561 B +41 B (+8%) 🔍
build/atomic-block-components/rating.js 565 B +41 B (+8%) 🔍
build/atomic-block-components/sku-frontend.js 389 B -1 B (0%)
build/atomic-block-components/stock-indicator-frontend.js 611 B +43 B (+8%) 🔍
build/atomic-block-components/stock-indicator.js 612 B +39 B (+7%) 🔍
build/attribute-filter-frontend.js 17.8 kB +29 B (0%)
build/attribute-filter.js 11.6 kB +227 B (+2%)
build/blocks-checkout-editor.js 10.7 kB +130 B (+1%)
build/blocks-checkout.js 20.1 kB +146 B (+1%)
build/cart-frontend.js 78.1 kB -463 B (-1%)
build/cart.js 45.4 kB +106 B (0%)
build/checkout-frontend.js 82.2 kB -348 B (0%)
build/checkout.js 47.7 kB +324 B (+1%)
build/featured-category.js 7.38 kB +84 B (+1%)
build/featured-product.js 9.55 kB +71 B (+1%)
build/handpicked-products.js 6.55 kB +518 B (+9%) 🔍
build/price-filter-frontend.js 14.2 kB -42 B (0%)
build/price-filter.js 9.41 kB +41 B (0%)
build/product-best-sellers.js 6.72 kB +535 B (+9%) 🔍
build/product-categories.js 3.38 kB -5 B (0%)
build/product-category.js 7.59 kB +523 B (+7%) 🔍
build/product-new.js 6.88 kB +532 B (+8%) 🔍
build/product-on-sale.js 7.23 kB +537 B (+8%) 🔍
build/product-search.js 2.68 kB +1 B (0%)
build/product-tag.js 6.69 kB +514 B (+8%) 🔍
build/product-top-rated.js 6.85 kB +527 B (+8%) 🔍
build/products-by-attribute.js 7.82 kB +526 B (+7%) 🔍
build/reviews-by-category.js 11.4 kB +137 B (+1%)
build/reviews-by-product.js 12.9 kB +137 B (+1%)
build/reviews-frontend.js 9.01 kB +15 B (0%)
build/single-product-frontend.js 37.8 kB -587 B (-2%)
build/single-product.js 9.88 kB +166 B (+2%)
build/vendors--atomic-block-components/price-frontend.js 6.51 kB -22 B (0%)
build/wc-blocks-data.js 10.9 kB -80 B (-1%)
build/wc-blocks-middleware.js 1.48 kB -4 B (0%)
build/wc-blocks-registry.js 2.75 kB +3 B (0%)
build/wc-blocks-shared-hocs.js 1.75 kB +1 B (0%)
build/wc-blocks-style-rtl.css 19.1 kB +10 B (0%)
build/wc-blocks-style.css 19.1 kB +12 B (0%)
build/wc-blocks-vendors.js 240 kB -2.28 kB (-1%)
build/wc-blocks.js 3.51 kB -5 B (0%)
build/wc-payment-method-stripe.js 12.3 kB -148 B (-1%)
build/wc-settings.js 2.93 kB -8 B (0%)
ℹ️ View Unchanged
Filename Size
build/atomic-block-components/sale-badge-frontend.js 470 B
build/atomic-block-components/sale-badge.js 474 B
build/atomic-block-components/sku.js 394 B
build/atomic-block-components/summary-frontend.js 906 B
build/atomic-block-components/summary.js 911 B
build/atomic-block-components/tag-list-frontend.js 467 B
build/atomic-block-components/tag-list.js 472 B
build/atomic-block-components/title-frontend.js 1.43 kB
build/atomic-block-components/title.js 1.28 kB
build/price-format.js 1.38 kB
build/wc-blocks-editor-style-rtl.css 14.9 kB
build/wc-blocks-editor-style.css 14.9 kB
build/wc-blocks-google-analytics.js 1.99 kB
build/wc-blocks-shared-context.js 1.54 kB
build/wc-blocks-vendors-style-rtl.css 1.05 kB
build/wc-blocks-vendors-style.css 1.05 kB
build/wc-payment-method-bacs.js 812 B
build/wc-payment-method-cheque.js 807 B
build/wc-payment-method-cod.js 903 B
build/wc-payment-method-paypal.js 844 B

compressed-size-action

@opr opr mentioned this pull request Jun 5, 2021
Copy link
Contributor

@ralucaStan ralucaStan left a 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.
Copy link
Contributor

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:
Copy link
Contributor

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.

Copy link
Contributor

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.

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 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.

docs/extensibility/available-filters.md Show resolved Hide resolved
// functionality when it comes to removing the coupon.
const cartCoupons = cartData.coupons.map( ( coupon ) => ( {
...coupon,
text: coupon.code,
Copy link
Contributor

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?

@@ -92,7 +89,7 @@ const TotalsDiscount = ( {
'Remove coupon "%s"',
'woo-gutenberg-products-block'
),
filteredCouponCode
cartCoupon.text
Copy link
Contributor

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?

Copy link
Contributor Author

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(
Copy link
Contributor

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" /> | * |
Copy link
Contributor

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
Copy link
Contributor

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

@ralucaStan ralucaStan self-requested a review July 1, 2021 12:48
Copy link
Contributor

@ralucaStan ralucaStan left a comment

Choose a reason for hiding this comment

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

Nice work!

@opr opr added this to the 5.5.0 milestone Jul 1, 2021
@opr opr merged commit 775cedd into trunk Jul 1, 2021
@opr opr deleted the fix/coupon-filters branch July 1, 2021 13:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: cart Issues related to the cart block. block: checkout Issues related to the checkout block. category: extensibility Work involving adding or updating extensibility. Useful to combine with other scopes impacted. focus: blocks Specific work involving or impacting how blocks behave. type: bug The issue/PR concerns a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding more than one coupon to the cart results in an error
2 participants