Skip to content

Only load Stripe JS on product page when required #2110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Fix - Error when changing payment method for a subscription with new checkout experience.
* Fix - Payment Requests are now updated correctly when updating items in the Cart Block.
* Add - Support for WooCommerce Pre-Orders with new checkout experience.
* Fix - Stripe JS is no longer loaded on cart and product pages when PRBs are disabled on those pages.
* Tweak - Update the minimum required PHP version to 7.0 to reflect our L-2 support policy.
* Fix - Add support for MYR (Malaysian ringgit) for Alipay.

Expand Down
8 changes: 8 additions & 0 deletions includes/class-wc-gateway-stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,14 @@ public function payment_scripts() {
return;
}

if ( is_product() && ! WC_Stripe_Helper::should_load_scripts_on_product_page() ) {
return;
}

if ( is_cart() && ! WC_Stripe_Helper::should_load_scripts_on_cart_page() ) {
return;
}

// If Stripe is not enabled bail.
if ( 'no' === $this->enabled ) {
return;
Expand Down
18 changes: 18 additions & 0 deletions includes/class-wc-stripe-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,4 +583,22 @@ public static function should_enqueue_in_current_tab_section( $tab, $section ) {

return true;
}

public static function should_load_scripts_on_product_page() {
$prb_locations = self::get_settings( null, 'payment_request_button_locations' ) ?? [ 'product', 'cart' ];
if ( ! in_array( 'product', $prb_locations, true ) ) {
return apply_filters( 'wc_stripe_load_scripts_on_product_page_when_prbs_disabled', true );
}

return true;
}

public static function should_load_scripts_on_cart_page() {
$prb_locations = self::get_settings( null, 'payment_request_button_locations' ) ?? [ 'product', 'cart' ];
if ( ! in_array( 'cart', $prb_locations, true ) ) {
return apply_filters( 'wc_stripe_load_scripts_on_cart_page_when_prbs_disabled', true );
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ public function payment_scripts() {
return;
}

if ( is_cart() && ! WC_Stripe_Helper::should_load_scripts_on_cart_page() ) {
return;
}

$asset_path = WC_STRIPE_PLUGIN_PATH . '/build/checkout_upe.asset.php';
$version = WC_STRIPE_VERSION;
$dependencies = [];
Expand Down
162 changes: 111 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ If you get stuck, you can ask for help in the Plugin Forum.
* Fix - Error when changing payment method for a subscription with new checkout experience.
* Fix - Payment Requests are now updated correctly when updating items in the Cart Block.
* Add - Support for WooCommerce Pre-Orders with new checkout experience.
* Fix - Stripe JS is no longer loaded on cart and product pages when PRBs are disabled on those pages.
* Tweak - Update the minimum required PHP version to 7.0 to reflect our L-2 support policy.
* Fix - Add support for MYR (Malaysian ringgit) for Alipay.

Expand Down