From 74f28ca921a39e36d65c6fbf543693ebb7751370 Mon Sep 17 00:00:00 2001
From: Pedro Silva
Date: Mon, 3 Jul 2023 11:40:37 +0100
Subject: [PATCH] Add filter
woocommerce_paypal_payments__button_disabled to disable PayPal
buttons on a given context.
---
.../modules/ContextBootstrap/CartBootstap.js | 13 ++++++
.../ContextBootstrap/CheckoutBootstap.js | 13 ++++++
.../ContextBootstrap/MiniCartBootstap.js | 13 ++++++
.../ppcp-button/src/Assets/SmartButton.php | 45 ++++++++++++++-----
4 files changed, 73 insertions(+), 11 deletions(-)
diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/CartBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/CartBootstap.js
index 72d28932b..cc613da83 100644
--- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/CartBootstap.js
+++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/CartBootstap.js
@@ -1,5 +1,6 @@
import CartActionHandler from '../ActionHandler/CartActionHandler';
import {setVisible} from "../Helper/Hiding";
+import {disable} from "../Helper/ButtonDisabler";
class CartBootstrap {
constructor(gateway, renderer, errorHandler) {
@@ -15,6 +16,13 @@ class CartBootstrap {
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', () => {
this.render();
@@ -44,6 +52,11 @@ class CartBootstrap {
return document.querySelector(this.gateway.button.wrapper) !== null;
}
+ shouldEnable() {
+ return this.shouldRender()
+ && this.gateway.button.is_disabled !== true;
+ }
+
render() {
const actionHandler = new CartActionHandler(
PayPalCommerceGateway,
diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/CheckoutBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/CheckoutBootstap.js
index 59c6197a3..a42902d9b 100644
--- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/CheckoutBootstap.js
+++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/CheckoutBootstap.js
@@ -5,6 +5,7 @@ import {
isSavedCardSelected, ORDER_BUTTON_SELECTOR,
PaymentMethods
} from "../Helper/CheckoutMethodState";
+import {disable} from "../Helper/ButtonDisabler";
class CheckoutBootstap {
constructor(gateway, renderer, messages, spinner, errorHandler) {
@@ -20,6 +21,13 @@ class CheckoutBootstap {
init() {
this.render();
+ if (!this.shouldEnable()) {
+ this.renderer.disableSmartButtons();
+ disable(this.gateway.button.wrapper);
+ disable(this.gateway.messages.wrapper);
+ return;
+ }
+
// Unselect saved card.
// 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,
@@ -51,6 +59,11 @@ class CheckoutBootstap {
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() {
if (!this.shouldRender()) {
return;
diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/MiniCartBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/MiniCartBootstap.js
index 443c9afe4..a391ee7cd 100644
--- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/MiniCartBootstap.js
+++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/MiniCartBootstap.js
@@ -1,4 +1,5 @@
import CartActionHandler from '../ActionHandler/CartActionHandler';
+import {disable} from "../Helper/ButtonDisabler";
class MiniCartBootstap {
constructor(gateway, renderer, errorHandler) {
@@ -16,6 +17,13 @@ class MiniCartBootstap {
);
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', () => {
this.render();
});
@@ -26,6 +34,11 @@ class MiniCartBootstap {
|| document.querySelector(this.gateway.hosted_fields.mini_cart_wrapper) !== null;
}
+ shouldEnable() {
+ return this.shouldRender()
+ && this.gateway.button.is_disabled !== true;
+ }
+
render() {
if (!this.shouldRender()) {
return;
diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php
index 68583f793..5a7341e77 100644
--- a/modules/ppcp-button/src/Assets/SmartButton.php
+++ b/modules/ppcp-button/src/Assets/SmartButton.php
@@ -1357,20 +1357,43 @@ class SmartButton implements SmartButtonInterface {
* @return bool
*/
protected function is_button_disabled(): bool {
- if ( 'product' !== $this->context() ) {
- return false;
+ $context = $this->context();
+
+ if ( 'product' === $context ) {
+ $product = wc_get_product();
+
+ /**
+ * Allows to decide if the button should be disabled for a given product
+ */
+ if ( ( $isDisabled = apply_filters(
+ 'woocommerce_paypal_payments_product_button_disabled',
+ null,
+ $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;
+ }
}
- $product = wc_get_product();
+ if ( ( $isDisabled = apply_filters(
+ 'woocommerce_paypal_payments_button_disabled',
+ null,
+ $context
+ ) ) !== null ) {
+ return $isDisabled;
+ }
- /**
- * Allows to decide if the button should be disabled for a given product
- */
- return apply_filters(
- 'woocommerce_paypal_payments_product_button_disabled',
- false,
- $product
- );
+ return false;
}
/**