Add filter woocommerce_paypal_payments_<context>_button_disabled to disable PayPal buttons on a given context.

This commit is contained in:
Pedro Silva 2023-07-03 11:40:37 +01:00
parent cdd4a69bf5
commit 74f28ca921
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
4 changed files with 73 additions and 11 deletions

View file

@ -1,5 +1,6 @@
import CartActionHandler from '../ActionHandler/CartActionHandler'; import CartActionHandler from '../ActionHandler/CartActionHandler';
import {setVisible} from "../Helper/Hiding"; import {setVisible} from "../Helper/Hiding";
import {disable} from "../Helper/ButtonDisabler";
class CartBootstrap { class CartBootstrap {
constructor(gateway, renderer, errorHandler) { constructor(gateway, renderer, errorHandler) {
@ -15,6 +16,13 @@ class CartBootstrap {
this.render(); this.render();
if (!this.shouldEnable()) {
this.renderer.disableSmartButtons();
disable(this.gateway.button.wrapper);
disable(this.gateway.messages.wrapper);
return;
}
jQuery(document.body).on('updated_cart_totals updated_checkout', () => { jQuery(document.body).on('updated_cart_totals updated_checkout', () => {
this.render(); this.render();
@ -44,6 +52,11 @@ class CartBootstrap {
return document.querySelector(this.gateway.button.wrapper) !== null; return document.querySelector(this.gateway.button.wrapper) !== null;
} }
shouldEnable() {
return this.shouldRender()
&& this.gateway.button.is_disabled !== true;
}
render() { render() {
const actionHandler = new CartActionHandler( const actionHandler = new CartActionHandler(
PayPalCommerceGateway, PayPalCommerceGateway,

View file

@ -5,6 +5,7 @@ import {
isSavedCardSelected, ORDER_BUTTON_SELECTOR, isSavedCardSelected, ORDER_BUTTON_SELECTOR,
PaymentMethods PaymentMethods
} from "../Helper/CheckoutMethodState"; } from "../Helper/CheckoutMethodState";
import {disable} from "../Helper/ButtonDisabler";
class CheckoutBootstap { class CheckoutBootstap {
constructor(gateway, renderer, messages, spinner, errorHandler) { constructor(gateway, renderer, messages, spinner, errorHandler) {
@ -20,6 +21,13 @@ class CheckoutBootstap {
init() { init() {
this.render(); this.render();
if (!this.shouldEnable()) {
this.renderer.disableSmartButtons();
disable(this.gateway.button.wrapper);
disable(this.gateway.messages.wrapper);
return;
}
// Unselect saved card. // Unselect saved card.
// WC saves form values, so with our current UI it would be a bit weird // WC saves form values, so with our current UI it would be a bit weird
// if the user paid with saved, then after some time tries to pay again, // if the user paid with saved, then after some time tries to pay again,
@ -51,6 +59,11 @@ class CheckoutBootstap {
return document.querySelector(this.gateway.button.wrapper) !== null || document.querySelector(this.gateway.hosted_fields.wrapper) !== null; return document.querySelector(this.gateway.button.wrapper) !== null || document.querySelector(this.gateway.hosted_fields.wrapper) !== null;
} }
shouldEnable() {
return this.shouldRender()
&& this.gateway.button.is_disabled !== true;
}
render() { render() {
if (!this.shouldRender()) { if (!this.shouldRender()) {
return; return;

View file

@ -1,4 +1,5 @@
import CartActionHandler from '../ActionHandler/CartActionHandler'; import CartActionHandler from '../ActionHandler/CartActionHandler';
import {disable} from "../Helper/ButtonDisabler";
class MiniCartBootstap { class MiniCartBootstap {
constructor(gateway, renderer, errorHandler) { constructor(gateway, renderer, errorHandler) {
@ -16,6 +17,13 @@ class MiniCartBootstap {
); );
this.render(); this.render();
if (!this.shouldEnable()) {
this.renderer.disableSmartButtons();
disable(this.gateway.button.wrapper);
disable(this.gateway.messages.wrapper);
return;
}
jQuery(document.body).on('wc_fragments_loaded wc_fragments_refreshed', () => { jQuery(document.body).on('wc_fragments_loaded wc_fragments_refreshed', () => {
this.render(); this.render();
}); });
@ -26,6 +34,11 @@ class MiniCartBootstap {
|| document.querySelector(this.gateway.hosted_fields.mini_cart_wrapper) !== null; || document.querySelector(this.gateway.hosted_fields.mini_cart_wrapper) !== null;
} }
shouldEnable() {
return this.shouldRender()
&& this.gateway.button.is_disabled !== true;
}
render() { render() {
if (!this.shouldRender()) { if (!this.shouldRender()) {
return; return;

View file

@ -1357,20 +1357,43 @@ class SmartButton implements SmartButtonInterface {
* @return bool * @return bool
*/ */
protected function is_button_disabled(): bool { protected function is_button_disabled(): bool {
if ( 'product' !== $this->context() ) { $context = $this->context();
return false;
}
if ( 'product' === $context ) {
$product = wc_get_product(); $product = wc_get_product();
/** /**
* Allows to decide if the button should be disabled for a given product * Allows to decide if the button should be disabled for a given product
*/ */
return apply_filters( if ( ( $isDisabled = apply_filters(
'woocommerce_paypal_payments_product_button_disabled', 'woocommerce_paypal_payments_product_button_disabled',
false, null,
$product $product )
); ) !== null) {
return $isDisabled;
}
} else {
$filterName = 'woocommerce_paypal_payments_'
. str_replace('-', '_', $context)
. '_button_disabled';
/**
* Allows to decide if the button should be disabled in a given context
*/
if ( ( $isDisabled = apply_filters( $filterName, null ) ) !== null ) {
return $isDisabled;
}
}
if ( ( $isDisabled = apply_filters(
'woocommerce_paypal_payments_button_disabled',
null,
$context
) ) !== null ) {
return $isDisabled;
}
return false;
} }
/** /**