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;
if ( ! this.isEligible ) {
jQuery( '#' + idButton ).hide();
jQuery( '#' + idMinicart ).hide();
jQuery( '#express-payment-method-ppcp-applepay' ).hide();
const hideContainers = [
// Payment button (Pay now, smart button block)
`#${ 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;
}
// 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.
const setupButtonEvents = ( id ) => {
document

View file

@ -443,15 +443,15 @@ class ApplePayButton implements ButtonInterface {
)
);
} else {
add_filter(
'woocommerce_payment_successful_result',
function ( array $result ) use ( $cart, $cart_item_key ) : array {
$this->clear_current_cart( $cart, $cart_item_key );
$this->reload_cart( $cart );
return $result;
}
);
}
add_filter(
'woocommerce_payment_successful_result',
function ( array $result ) use ( $cart, $cart_item_key ) : array {
$this->clear_current_cart( $cart, $cart_item_key );
$this->reload_cart( $cart );
return $result;
}
);
}
}
WC()->checkout()->process_checkout();
@ -953,6 +953,7 @@ class ApplePayButton implements ButtonInterface {
$render_placeholder,
function () {
$this->applepay_button();
$this->hide_gateway_until_eligible();
},
21
);
@ -997,6 +998,19 @@ class ApplePayButton implements ButtonInterface {
<?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.
*