Hide Apple Pay gateway on ineligible devices

This commit is contained in:
Philipp Stracker 2024-07-24 15:21:09 +02:00
parent 43d7e0788f
commit 3bb0496123
No known key found for this signature in database
2 changed files with 44 additions and 12 deletions

View file

@ -150,13 +150,31 @@ class ApplePayButton {
const idButton = this.buttonConfig.button.wrapper; const idButton = this.buttonConfig.button.wrapper;
if ( ! this.isEligible ) { if ( ! this.isEligible ) {
jQuery( '#' + idButton ).hide(); const hideContainers = [
jQuery( '#' + idMinicart ).hide(); // Payment button (Pay now, smart button block)
jQuery( '#express-payment-method-ppcp-applepay' ).hide(); `#${ idButton }`,
// Mini Cart button
`#${ idMinicart }`,
// Block Checkout: Express checkout button.
'#express-payment-method-ppcp-applepay',
];
hideContainers.forEach( ( selector ) => {
const elements = document.querySelectorAll( selector );
elements.forEach( ( element ) => {
element.style.display = 'none';
} );
} );
return; return;
} }
// Classic Checkout: Make the Apple Pay gateway visible.
document
.querySelectorAll( 'style#ppcp-hide-apple-pay' )
.forEach( ( el ) => el.remove() );
// Add click-handler to the button. // Add click-handler to the button.
const setupButtonEvents = ( id ) => { const setupButtonEvents = ( id ) => {
document document

View file

@ -443,15 +443,15 @@ class ApplePayButton implements ButtonInterface {
) )
); );
} else { } else {
add_filter( add_filter(
'woocommerce_payment_successful_result', 'woocommerce_payment_successful_result',
function ( array $result ) use ( $cart, $cart_item_key ) : array { function ( array $result ) use ( $cart, $cart_item_key ) : array {
$this->clear_current_cart( $cart, $cart_item_key ); $this->clear_current_cart( $cart, $cart_item_key );
$this->reload_cart( $cart ); $this->reload_cart( $cart );
return $result; return $result;
} }
); );
} }
} }
WC()->checkout()->process_checkout(); WC()->checkout()->process_checkout();
@ -953,6 +953,7 @@ class ApplePayButton implements ButtonInterface {
$render_placeholder, $render_placeholder,
function () { function () {
$this->applepay_button(); $this->applepay_button();
$this->hide_gateway_until_eligible();
}, },
21 21
); );
@ -997,6 +998,19 @@ class ApplePayButton implements ButtonInterface {
<?php <?php
} }
/**
* Outputs an inline CSS style that hides the Apple Pay gateway (on Classic Checkout).
* The style is removed by `ApplepayButton.js` once the eligibility of the payment method
* is confirmed.
*
* @return void
*/
protected function hide_gateway_until_eligible(): void {
?>
<style id="ppcp-hide-apple-pay">.wc_payment_method.payment_method_ppcp-applepay{display:none}</style>
<?php
}
/** /**
* Enqueues the scripts. * Enqueues the scripts.
* *