-
Notifications
You must be signed in to change notification settings - Fork 69
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
Refresh page when ECE is dismissed #9888
Changes from 27 commits
4a45962
e1be2e2
21e38d7
c03c229
ffa1286
41d527c
46defc4
0db8da6
84ced5a
ee2b1d2
eaf650d
8cfe829
c04f201
752a88e
7d8c479
b399fb6
e57f86a
5ca6d08
959eb6e
0e665d7
f68ea83
b5a0e47
1831e70
64f118e
86f69cf
c0728be
ce9aa79
b47ab71
08d18fc
42dad17
240576d
ec81858
80f40dc
20c4e42
9dd5e1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: add | ||
|
||
Refresh the cart and checkout pages when ECE is dismissed and the shipping options were modified in the payment sheet. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* global jQuery */ | ||
/** | ||
* External dependencies | ||
*/ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { normalizeShippingAddress, getExpressCheckoutData } from '.'; | ||
|
||
/* | ||
* Updates a field in a form with a new value. | ||
* | ||
* @param {String} formSelector - The selector for the form containing the field. | ||
* @param {Object} fieldName - The name of the field to update. | ||
* @param {Object} value - The new value for the field. | ||
*/ | ||
const updateShortcodeField = ( formSelector, fieldName, value ) => { | ||
const field = document.querySelector( | ||
`${ formSelector } [name="${ fieldName }"]` | ||
); | ||
if ( field ) { | ||
field.value = value; | ||
jQuery( field ).trigger( 'change' ).trigger( 'close' ); | ||
} | ||
}; | ||
|
||
/** | ||
* Updates the WooCommerce Blocks shipping UI to reflect a new shipping address. | ||
* | ||
* @param {Object} eventAddress - The shipping address returned by the payment event. | ||
*/ | ||
export const updateBlocksShippingUI = ( eventAddress ) => { | ||
wp?.data | ||
?.dispatch( 'wc/store/cart' ) | ||
?.setShippingAddress( normalizeShippingAddress( eventAddress ) ); | ||
}; | ||
|
||
/** | ||
* Updates the WooCommerce shortcode cart/checkout shipping UI to reflect a new shipping address. | ||
* | ||
* @param {Object} eventAddress - The shipping address returned by the payment event. | ||
*/ | ||
export const updateShortcodeShippingUI = ( eventAddress ) => { | ||
const context = getExpressCheckoutData( 'button_context' ); | ||
const address = normalizeShippingAddress( eventAddress ); | ||
|
||
const keys = [ 'country', 'state', 'city', 'postcode' ]; | ||
|
||
if ( context === 'cart' ) { | ||
keys.forEach( ( key ) => { | ||
if ( address[ key ] ) { | ||
updateShortcodeField( | ||
'form.woocommerce-shipping-calculator', | ||
`calc_shipping_${ key }`, | ||
address[ key ] | ||
); | ||
} | ||
} ); | ||
document | ||
.querySelector( | ||
'form.woocommerce-shipping-calculator [name="calc_shipping"]' | ||
) | ||
?.click(); | ||
} else if ( context === 'checkout' ) { | ||
keys.forEach( ( key ) => { | ||
if ( address[ key ] ) { | ||
updateShortcodeField( | ||
'form.woocommerce-checkout', | ||
`billing_${ key }`, | ||
address[ key ] | ||
); | ||
} | ||
} ); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,12 @@ import { applyFilters } from '@wordpress/hooks'; | |
/** | ||
* Internal dependencies | ||
*/ | ||
import { getErrorMessageFromNotice, getExpressCheckoutData } from './utils'; | ||
import { | ||
getErrorMessageFromNotice, | ||
getExpressCheckoutData, | ||
updateBlocksShippingUI, | ||
updateShortcodeShippingUI, | ||
} from './utils'; | ||
import { | ||
trackExpressCheckoutButtonClick, | ||
trackExpressCheckoutButtonLoad, | ||
|
@@ -24,6 +29,7 @@ import { | |
transformPrice, | ||
} from './transformers/wc-to-stripe'; | ||
|
||
let lastSelectedAddress = null; | ||
let cartApi = new ExpressCheckoutCartApi(); | ||
export const setCartApiHandler = ( handler ) => ( cartApi = handler ); | ||
export const getCartApiHandler = () => cartApi; | ||
|
@@ -56,6 +62,9 @@ export const shippingAddressChangeHandler = async ( event, elements ) => { | |
cartData.totals | ||
), | ||
} ); | ||
|
||
lastSelectedAddress = event.address; | ||
|
||
event.resolve( { | ||
shippingRates: transformCartDataForShippingRates( cartData ), | ||
lineItems: transformCartDataForDisplayItems( cartData ), | ||
|
@@ -216,5 +225,26 @@ export const onCompletePaymentHandler = () => { | |
}; | ||
|
||
export const onCancelHandler = () => { | ||
const context = getExpressCheckoutData( 'button_context' ); | ||
const isBlocks = getExpressCheckoutData( 'has_block' ); | ||
|
||
/* | ||
* CA and GB addresses are redacted and are causing errors until WooCommerce is able to | ||
* handle redacted addresses. | ||
* https://developers.google.com/pay/api/web/reference/response-objects#IntermediateAddress | ||
*/ | ||
if ( | ||
[ 'cart', 'checkout' ].includes( context ) && | ||
lastSelectedAddress && | ||
! [ 'CA', 'GB' ].includes( lastSelectedAddress.country ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Let's put this array in a variable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this mirrors the code in the other file, I’ll wait until we complete the review process before copying the changes over to tokenized carts. You can focus your review on the code outside tokenized carts for now. |
||
) { | ||
if ( isBlocks ) { | ||
updateBlocksShippingUI( lastSelectedAddress ); | ||
} else { | ||
updateShortcodeShippingUI( lastSelectedAddress ); | ||
} | ||
} | ||
|
||
lastSelectedAddress = null; | ||
unblockUI(); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* global jQuery */ | ||
/** | ||
* External dependencies | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious: is it necessary to include this “external dependencies” comment even if we’re not importing a single module? |
||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { normalizeShippingAddress, getExpressCheckoutData } from '.'; | ||
|
||
/* | ||
* Updates a field in a form with a new value. | ||
* | ||
* @param {String} formSelector - The selector for the form containing the field. | ||
* @param {Object} fieldName - The name of the field to update. | ||
* @param {Object} value - The new value for the field. | ||
*/ | ||
const updateShortcodeField = ( formSelector, fieldName, value ) => { | ||
const field = document.querySelector( | ||
`${ formSelector } [name="${ fieldName }"]` | ||
); | ||
if ( field ) { | ||
field.value = value; | ||
jQuery( field ).trigger( 'change' ).trigger( 'close' ); | ||
} | ||
}; | ||
|
||
/** | ||
* Updates the WooCommerce Blocks shipping UI to reflect a new shipping address. | ||
* | ||
* @param {Object} eventAddress - The shipping address returned by the payment event. | ||
*/ | ||
export const updateBlocksShippingUI = ( eventAddress ) => { | ||
wp?.data | ||
?.dispatch( 'wc/store/cart' ) | ||
?.setShippingAddress( normalizeShippingAddress( eventAddress ) ); | ||
}; | ||
|
||
/** | ||
* Updates the WooCommerce shortcode cart/checkout shipping UI to reflect a new shipping address. | ||
* | ||
* @param {Object} eventAddress - The shipping address returned by the payment event. | ||
*/ | ||
export const updateShortcodeShippingUI = ( eventAddress ) => { | ||
const context = getExpressCheckoutData( 'button_context' ); | ||
const address = normalizeShippingAddress( eventAddress ); | ||
|
||
const keys = [ 'country', 'state', 'city', 'postcode' ]; | ||
|
||
if ( context === 'cart' ) { | ||
keys.forEach( ( key ) => { | ||
if ( address[ key ] ) { | ||
updateShortcodeField( | ||
'form.woocommerce-shipping-calculator', | ||
`calc_shipping_${ key }`, | ||
address[ key ] | ||
); | ||
} | ||
} ); | ||
document | ||
.querySelector( | ||
'form.woocommerce-shipping-calculator [name="calc_shipping"]' | ||
) | ||
?.click(); | ||
} else if ( context === 'checkout' ) { | ||
keys.forEach( ( key ) => { | ||
if ( address[ key ] ) { | ||
updateShortcodeField( | ||
'form.woocommerce-checkout', | ||
`billing_${ key }`, | ||
address[ key ] | ||
); | ||
} | ||
} ); | ||
} | ||
}; |
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.
Can we move this array, along with the accompanying comment, into a self-explanatory variable, either within this function or at a higher level? As this list will likely need to be updated with more countries in the future, using a variable name and relocating it would improve readability and maintainability.