From 5e6d7f97d25e0d4360d12df773b7b859bb37650a Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Thu, 4 Jan 2024 17:30:50 +0000 Subject: [PATCH] Clear PUI and DCC product status on woocommerce_paypal_payments_clear_apm_product_status event --- .../src/Helper/DCCProductStatus.php | 26 +++++++++++++++++++ .../Helper/PayUponInvoiceProductStatus.php | 26 +++++++++++++++++++ .../ppcp-wc-gateway/src/WCGatewayModule.php | 16 ++++++++++++ 3 files changed, 68 insertions(+) diff --git a/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php b/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php index 772a76e3a..3ca5b9998 100644 --- a/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php +++ b/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php @@ -186,4 +186,30 @@ class DCCProductStatus { return $this->has_request_failure; } + /** + * Clears the persisted result to force a recheck. + * + * @param Settings|null $settings The settings object. + * We accept a Settings object to don't override other sequential settings that are being updated elsewhere. + * @return void + */ + public function clear( Settings $settings = null ): void { + if ( null === $settings ) { + $settings = $this->settings; + } + + // Unset check stored in memory. + $this->current_status_cache = null; + + // Unset settings flag. + $settings_key = 'products_dcc_enabled'; + if ( $settings->has( $settings_key ) ) { + $settings->set( $settings_key, false ); + $settings->persist(); + } + + // Delete cached value. + $this->cache->delete( self::DCC_STATUS_CACHE_KEY ); + } + } diff --git a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php index f1a3221eb..dfbcb0380 100644 --- a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php +++ b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php @@ -173,4 +173,30 @@ class PayUponInvoiceProductStatus { return $this->has_request_failure; } + /** + * Clears the persisted result to force a recheck. + * + * @param Settings|null $settings The settings object. + * We accept a Settings object to don't override other sequential settings that are being updated elsewhere. + * @return void + */ + public function clear( Settings $settings = null ): void { + if ( null === $settings ) { + $settings = $this->settings; + } + + // Unset check stored in memory. + $this->current_status_cache = null; + + // Unset settings flag. + $settings_key = 'products_pui_enabled'; + if ( $settings->has( $settings_key ) ) { + $settings->set( $settings_key, false ); + $settings->persist(); + } + + // Delete cached value. + $this->cache->delete( self::PUI_STATUS_CACHE_KEY ); + } + } diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index 6af49c778..79a6dc127 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -430,6 +430,22 @@ class WCGatewayModule implements ModuleInterface { $c->get( 'wcgateway.cli.settings.command' ) ); } + + // Clears product status when appropriate. + add_action( + 'woocommerce_paypal_payments_clear_apm_product_status', + function( Settings $settings = null ) use ( $c ): void { + $dcc_product_status = $c->get( 'wcgateway.helper.dcc-product-status' ); + if ( $dcc_product_status instanceof DCCProductStatus ) { + $dcc_product_status->clear( $settings ); + } + + $pui_product_status = $c->get( 'wcgateway.pay-upon-invoice-product-status' ); + if ( $pui_product_status instanceof PayUponInvoiceProductStatus ) { + $pui_product_status->clear( $settings ); + } + } + ); } /**