From 2d3706291bf9f67d5e4ed895351a70a0c9b6903a Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Thu, 6 Feb 2025 14:25:35 +0100 Subject: [PATCH] Add filter for credit card gateway enabled --- modules/ppcp-settings/src/SettingsModule.php | 17 +++++++++++ .../src/Gateway/CreditCardGateway.php | 29 ++++++++++++++----- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/modules/ppcp-settings/src/SettingsModule.php b/modules/ppcp-settings/src/SettingsModule.php index c13941c45..281c674af 100644 --- a/modules/ppcp-settings/src/SettingsModule.php +++ b/modules/ppcp-settings/src/SettingsModule.php @@ -394,6 +394,23 @@ class SettingsModule implements ServiceModule, ExecutableModule { 2 ); + add_filter( + 'woocommerce_paypal_payments_credit_card_gateway_form_fields', + function( array $form_fields ) { + $form_fields['enabled'] = array( + 'title' => __( 'Enable/Disable', 'woocommerce-paypal-payments' ), + 'type' => 'checkbox', + 'desc_tip' => true, + 'description' => __( 'Once enabled, the Credit Card option will show up in the checkout.', 'woocommerce-paypal-payments' ), + 'label' => __( 'Enable Advanced Card Processing', 'woocommerce-paypal-payments' ), + 'default' => 'no', + ); + + return $form_fields; + } + ); + add_filter( 'woocommerce_paypal_payments_credit_card_gateway_should_update_enabled', '__return_false' ); + add_filter( 'woocommerce_paypal_payments_credit_card_gateway_title', function( string $title, WC_Payment_Gateway $gateway ) { diff --git a/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php b/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php index 90457ad77..2f1e997b5 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php @@ -279,9 +279,9 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { 'woocommerce-paypal-payments' ); - $this->title = apply_filters( 'woocommerce_paypal_payments_credit_card_gateway_title', $this->dcc_configuration->gateway_title(), $this ); - $this->description = apply_filters( 'woocommerce_paypal_payments_credit_card_gateway_description', $this->dcc_configuration->gateway_description(), $this ); - $this->card_icons = $card_icons; + $this->title = apply_filters( 'woocommerce_paypal_payments_credit_card_gateway_title', $this->dcc_configuration->gateway_title(), $this ); + $this->description = apply_filters( 'woocommerce_paypal_payments_credit_card_gateway_description', $this->dcc_configuration->gateway_description(), $this ); + $this->card_icons = $card_icons; $this->init_form_fields(); $this->init_settings(); @@ -299,10 +299,13 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { * Initialize the form fields. */ public function init_form_fields() { - $this->form_fields = array( - 'ppcp' => array( - 'type' => 'ppcp', - ), + $this->form_fields = apply_filters( + 'woocommerce_paypal_payments_credit_card_gateway_form_fields', + array( + 'ppcp' => array( + 'type' => 'ppcp', + ), + ) ); } @@ -583,6 +586,10 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { public function init_settings() { parent::init_settings(); + if ( ! apply_filters( 'woocommerce_paypal_payments_credit_card_gateway_should_update_enabled', true ) ) { + return; + } + // looks like in some cases WC uses this field instead of get_option. $this->enabled = $this->is_enabled() ? 'yes' : ''; } @@ -595,6 +602,10 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { * @return mixed */ public function get_option( $key, $empty_value = null ) { + if ( ! apply_filters( 'woocommerce_paypal_payments_credit_card_gateway_should_update_enabled', true ) ) { + return parent::get_option( $key, $empty_value ); + } + if ( 'enabled' === $key ) { return $this->is_enabled(); } @@ -612,6 +623,10 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { public function update_option( $key, $value = '' ) { $ret = parent::update_option( $key, $value ); + if ( ! apply_filters( 'woocommerce_paypal_payments_credit_card_gateway_should_update_enabled', true ) ) { + return $ret; + } + if ( 'enabled' === $key ) { $this->config->set( 'dcc_enabled', 'yes' === $value );