From 47fa01110c8266804b87d3067a0e31830d69f4b6 Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Thu, 13 Feb 2025 19:57:26 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Allow=20WC=20Gateway=20changes=20vi?= =?UTF-8?q?s=20PaymentSettings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Data/PaymentSettings.php | 61 ++++++++++++++++++- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-settings/src/Data/PaymentSettings.php b/modules/ppcp-settings/src/Data/PaymentSettings.php index ac80e66fe..424aec26e 100644 --- a/modules/ppcp-settings/src/Data/PaymentSettings.php +++ b/modules/ppcp-settings/src/Data/PaymentSettings.php @@ -52,7 +52,11 @@ class PaymentSettings extends AbstractDataModel { public function save() : void { parent::save(); - foreach ( $this->unsaved_gateways as $gateway_id => $gateway ) { + foreach ( $this->unsaved_gateways as $gateway ) { + $gateway->settings['enabled'] = $gateway->enabled; + $gateway->settings['title'] = $gateway->title; + $gateway->settings['description'] = $gateway->description; + update_option( $gateway->get_option_key(), $gateway->settings ); } @@ -81,7 +85,7 @@ class PaymentSettings extends AbstractDataModel { if ( $gateway ) { $gateway->enabled = wc_bool_to_string( $is_enabled ); - $this->unsaved_gateways[ $gateway->id ] = $gateway; + $this->modified_gateway( $gateway ); } } } @@ -113,6 +117,40 @@ class PaymentSettings extends AbstractDataModel { return false; } + /** + * Updates the payment method title. + * + * @param string $method_id ID of the payment method. + * @param string $title The new title. + * @return void + */ + public function set_method_title( string $method_id, string $title ) : void { + $gateway = $this->get_gateway( $method_id ); + + if ( $gateway ) { + $gateway->title = $title; + + $this->modified_gateway( $gateway ); + } + } + + /** + * Updates the payment method description. + * + * @param string $method_id ID of the payment method. + * @param string $description The new description. + * @return void + */ + public function set_method_description( string $method_id, string $description ) : void { + $gateway = $this->get_gateway( $method_id ); + + if ( $gateway ) { + $gateway->description = $description; + + $this->modified_gateway( $gateway ); + } + } + /** * Get PayPal show logo. * @@ -240,6 +278,23 @@ class PaymentSettings extends AbstractDataModel { $gateways = WC()->payment_gateways()->payment_gateways(); - return isset( $gateways[ $method_id ] ) ? $gateways[ $method_id ] : null; + if ( ! isset( $gateways[ $method_id ] ) ) { + return null; + } + + $gateway = $gateways[ $method_id ]; + $gateway->init_form_fields(); + + return $gateway; + } + + /** + * Store the gateway object for later saving. + * + * @param WC_Payment_Gateway $gateway The gateway object. + * @return void + */ + private function modified_gateway( WC_Payment_Gateway $gateway ) : void { + $this->unsaved_gateways[ $gateway->id ] = $gateway; } }