Skip to content

Commit 3c2b21f

Browse files
committed
Fix React error when the Express Checkout's container element is not found. (#9420)
1 parent 12a3289 commit 3c2b21f

File tree

3 files changed

+14
-34
lines changed

3 files changed

+14
-34
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fix
3+
4+
Create div container element with JS dynamically.

client/express-checkout/utils/checkPaymentMethodIsAvailable.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ import { getUPEConfig } from 'wcpay/utils/checkout';
1515

1616
export const checkPaymentMethodIsAvailable = memoize(
1717
( paymentMethod, cart, resolve ) => {
18-
const root = ReactDOM.createRoot(
19-
document.getElementById(
20-
`express-checkout-check-availability-container-${ paymentMethod }`
21-
)
22-
);
18+
// Create the DIV container on the fly
19+
const containerEl = document.createElement( 'div' );
20+
21+
// Ensure the element is hidden and doesn’t interfere with the page layout.
22+
containerEl.style.display = 'none';
23+
24+
document.querySelector( 'body' ).appendChild( containerEl );
25+
26+
const root = ReactDOM.createRoot( containerEl );
2327

2428
const api = new WCPayAPI(
2529
{
@@ -71,6 +75,7 @@ export const checkPaymentMethodIsAvailable = memoize(
7175
}
7276
resolve( canMakePayment );
7377
root.unmount();
78+
containerEl.remove();
7479
} }
7580
/>
7681
</Elements>

includes/express-checkout/class-wc-payments-express-checkout-button-display-handler.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ public function init() {
9595
$is_woopay_enabled = WC_Payments_Features::is_woopay_enabled();
9696
$is_payment_request_enabled = 'yes' === $this->gateway->get_option( 'payment_request' );
9797

98-
if ( $is_payment_request_enabled ) {
99-
$this->add_html_container_for_test_express_checkout_buttons();
100-
}
101-
10298
if ( $is_woopay_enabled || $is_payment_request_enabled ) {
10399
add_action( 'wc_ajax_wcpay_add_to_cart', [ $this->express_checkout_ajax_handler, 'ajax_add_to_cart' ] );
104100
add_action( 'wc_ajax_wcpay_empty_cart', [ $this->express_checkout_ajax_handler, 'ajax_empty_cart' ] );
@@ -178,31 +174,6 @@ public function add_order_attribution_inputs() {
178174
echo '<wc-order-attribution-inputs id="wcpay-express-checkout__order-attribution-inputs"></wc-order-attribution-inputs>';
179175
}
180176

181-
182-
/**
183-
* Add HTML containers to be used by the Express Checkout buttons that check if the payment method is available.
184-
*
185-
* @return void
186-
*/
187-
private function add_html_container_for_test_express_checkout_buttons() {
188-
add_filter(
189-
'the_content',
190-
function ( $content ) {
191-
$supported_payment_methods = [ 'applePay' , 'googlePay' ];
192-
// Restrict adding these HTML containers to only the necessary pages.
193-
if ( $this->express_checkout_helper->is_checkout() || $this->express_checkout_helper->is_cart() ) {
194-
foreach ( $supported_payment_methods as $value ) {
195-
// The inline styles ensure that the HTML elements don't occupy space on the page.
196-
$content = '<div id="express-checkout-check-availability-container-' . $value . '" style="height: 0; float:left; opacity: 0; pointer-events: none;"></div>' . $content;
197-
}
198-
}
199-
return $content;
200-
},
201-
10,
202-
1
203-
);
204-
}
205-
206177
/**
207178
* Check if the pay-for-order flow is supported.
208179
*

0 commit comments

Comments
 (0)