diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index 9a78baacaa9..ea22c5231aa 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -310,6 +310,7 @@ class WC_Install { self::create_files(); self::maybe_create_pages(); self::maybe_set_activation_transients(); + self::set_paypal_standard_load_eligibility(); self::update_wc_version(); self::maybe_update_db_version(); @@ -1635,6 +1636,18 @@ CREATE TABLE {$wpdb->prefix}wc_reserved_stock ( ob_end_clean(); } } + + /** + * Sets whether PayPal Standard will be loaded on install. + * + * @since 5.5.0 + */ + private static function set_paypal_standard_load_eligibility() { + // Initiating the payment gateways sets the flag. + if ( class_exists( 'WC_Gateway_Paypal' ) ) { + ( new WC_Gateway_Paypal() )->should_load(); + } + } } WC_Install::init(); diff --git a/includes/class-wc-payment-gateways.php b/includes/class-wc-payment-gateways.php index c6114ef68c7..04520322ee0 100644 --- a/includes/class-wc-payment-gateways.php +++ b/includes/class-wc-payment-gateways.php @@ -78,9 +78,12 @@ class WC_Payment_Gateways { 'WC_Gateway_BACS', 'WC_Gateway_Cheque', 'WC_Gateway_COD', - 'WC_Gateway_Paypal', ); + if ( $this->should_load_paypal_standard() ) { + $load_gateways[] = 'WC_Gateway_Paypal'; + } + // Filter. $load_gateways = apply_filters( 'woocommerce_payment_gateways', $load_gateways ); @@ -219,4 +222,15 @@ class WC_Payment_Gateways { update_option( 'woocommerce_gateway_order', $order ); } + + /** + * Determines if PayPal Standard should be loaded. + * + * @since 5.5.0 + * @return bool Whether PayPal Standard should be loaded or not. + */ + protected function should_load_paypal_standard() { + $paypal = new WC_Gateway_Paypal(); + return $paypal->should_load(); + } } diff --git a/includes/gateways/paypal/class-wc-gateway-paypal.php b/includes/gateways/paypal/class-wc-gateway-paypal.php index 68a8095ad22..e6390151214 100644 --- a/includes/gateways/paypal/class-wc-gateway-paypal.php +++ b/includes/gateways/paypal/class-wc-gateway-paypal.php @@ -473,4 +473,42 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway { return $text; } + + /** + * Determines whether PayPal Standard should be loaded or not. + * + * By default PayPal Standard isn't loaded on new installs or on existing sites which haven't set up the gateway. + * + * @since 5.5.0 + * + * @return bool Whether PayPal Standard should be loaded. + */ + public function should_load() { + $option_key = '_should_load'; + $should_load = $this->get_option( $option_key ); + + if ( '' === $should_load ) { + + // New installs without PayPal Standard enabled don't load it. + if ( 'no' === $this->enabled && WC_Install::is_new_install() ) { + $should_load = false; + } else { + $should_load = true; + } + + $this->update_option( $option_key, wc_bool_to_string( $should_load ) ); + } else { + $should_load = wc_string_to_bool( $should_load ); + } + + /** + * Allow third-parties to filter whether PayPal Standard should be loaded or not. + * + * @since 5.5.0 + * + * @param bool $should_load Whether PayPal Standard should be loaded. + * @param WC_Gateway_Paypal $this The WC_Gateway_Paypal instance. + */ + return apply_filters( 'woocommerce_should_load_paypal_standard', $should_load, $this ); + } } diff --git a/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js b/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js index 46bf8121eff..812d7385d25 100644 --- a/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js +++ b/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js @@ -69,14 +69,6 @@ const runCheckoutPageTest = () => { // Verify that settings have been saved await verifyCheckboxIsSet('#woocommerce_cod_enabled'); - // Enable PayPal payment method - await merchant.openSettings('checkout', 'paypal'); - await setCheckbox('#woocommerce_paypal_enabled'); - await settingsPageSaveChanges(); - - // Verify that settings have been saved - await verifyCheckboxIsSet('#woocommerce_paypal_enabled'); - await merchant.logout(); }); @@ -93,7 +85,6 @@ const runCheckoutPageTest = () => { await shopper.goToCheckout(); await shopper.productIsInCheckout(simpleProductName, `2`, twoProductPrice, twoProductPrice); - await expect(page).toClick('.wc_payment_method label', {text: 'PayPal'}); await expect(page).toClick('.wc_payment_method label', {text: 'Direct bank transfer'}); await expect(page).toClick('.wc_payment_method label', {text: 'Cash on delivery'}); }); diff --git a/tests/legacy/bootstrap.php b/tests/legacy/bootstrap.php index fd8bfb96dce..3b648590f14 100644 --- a/tests/legacy/bootstrap.php +++ b/tests/legacy/bootstrap.php @@ -58,6 +58,9 @@ class WC_Unit_Tests_Bootstrap { // load test function so tests_add_filter() is available. require_once $this->wp_tests_dir . '/includes/functions.php'; + // Always load PayPal Standard for unit tests. + tests_add_filter( 'woocommerce_should_load_paypal_standard', '__return_true' ); + // load WC. tests_add_filter( 'muplugins_loaded', array( $this, 'load_wc' ) );