Skip to content
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

Fix duplicated enqueued scripts in shortcode checkout #8954

Merged
merged 5 commits into from
Jun 18, 2024
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
4 changes: 4 additions & 0 deletions changelog/fix-8863-duplicated-enqueued-scripts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Fix WooPay OTP modal not rendering on the shortcode checkout if BNPL methods are available.
44 changes: 25 additions & 19 deletions includes/class-wc-payments-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public function init_hooks() {
* Registers all scripts, necessary for the gateway.
*/
public function register_scripts() {
if ( wp_script_is( 'wcpay-upe-checkout', 'enqueued' ) ) {
return;
}
// Register Stripe's JavaScript using the same ID as the Stripe Gateway plugin. This prevents this JS being
// loaded twice in the event a site has both plugins enabled. We still run the risk of different plugins
// loading different versions however. If Stripe release a v4 of their JavaScript, we could consider
Expand Down Expand Up @@ -147,7 +150,8 @@ public function register_scripts_for_zero_order_total() {
! WC()->cart->is_empty() &&
! WC()->cart->needs_payment() &&
is_checkout() &&
! has_block( 'woocommerce/checkout' )
! has_block( 'woocommerce/checkout' ) &&
! wp_script_is( 'wcpay-upe-checkout', 'enqueued' )
) {
WC_Payments::get_gateway()->tokenization_script();
$script_handle = 'wcpay-upe-checkout';
Expand Down Expand Up @@ -360,28 +364,30 @@ public function payment_fields() {
* but we need `$this->get_payment_fields_js_config` to be called
* before `$this->saved_payment_methods()`.
*/
$payment_fields = $this->get_payment_fields_js_config();
wp_enqueue_script( 'wcpay-upe-checkout' );
add_action(
'wp_footer',
function () use ( $payment_fields ) {
wp_localize_script( 'wcpay-upe-checkout', 'wcpay_upe_config', $payment_fields );
if ( ! wp_script_is( 'wcpay-upe-checkout', 'enqueued' ) ) {
$payment_fields = $this->get_payment_fields_js_config();
wp_enqueue_script( 'wcpay-upe-checkout' );
add_action(
'wp_footer',
function () use ( $payment_fields ) {
wp_localize_script( 'wcpay-upe-checkout', 'wcpay_upe_config', $payment_fields );
}
);

$prepared_customer_data = $this->customer_service->get_prepared_customer_data();
if ( ! empty( $prepared_customer_data ) ) {
wp_localize_script( 'wcpay-upe-checkout', 'wcpayCustomerData', $prepared_customer_data );
}
);

$prepared_customer_data = $this->customer_service->get_prepared_customer_data();
if ( ! empty( $prepared_customer_data ) ) {
wp_localize_script( 'wcpay-upe-checkout', 'wcpayCustomerData', $prepared_customer_data );
WC_Payments_Utils::enqueue_style(
'wcpay-upe-checkout',
plugins_url( 'dist/checkout.css', WCPAY_PLUGIN_FILE ),
[],
WC_Payments::get_file_version( 'dist/checkout.css' ),
'all'
);
}

WC_Payments_Utils::enqueue_style(
'wcpay-upe-checkout',
plugins_url( 'dist/checkout.css', WCPAY_PLUGIN_FILE ),
[],
WC_Payments::get_file_version( 'dist/checkout.css' ),
'all'
);

// Output the form HTML.
?>
<?php if ( ! empty( $this->gateway->get_description() ) ) : ?>
Expand Down
4 changes: 4 additions & 0 deletions includes/fraud-prevention/class-fraud-prevention-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public static function get_instance( $session = null, $gateway = null ): self {
* @return void
*/
public static function maybe_append_fraud_prevention_token() {
if ( wp_script_is( self::TOKEN_NAME, 'enqueued' ) ) {
return;
}

// Check session first before trying to append the token.
if ( ! WC()->session ) {
return;
Expand Down
Loading