Merge pull request #29971 from woocommerce/hide-pypl-on-new-sites

Hide PayPal Standard on new installs
This commit is contained in:
Vedanshu Jain 2021-06-17 19:40:53 +05:30 committed by GitHub
commit 39c057f636
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 10 deletions

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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 );
}
}

View File

@ -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'});
});

View File

@ -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' ) );