diff --git a/changelog/fix-8863-duplicated-enqueued-scripts b/changelog/fix-8863-duplicated-enqueued-scripts new file mode 100644 index 00000000000..1f27da5b3aa --- /dev/null +++ b/changelog/fix-8863-duplicated-enqueued-scripts @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Fix WooPay OTP modal not rendering on the shortcode checkout if BNPL methods are available. diff --git a/includes/class-wc-payments-checkout.php b/includes/class-wc-payments-checkout.php index ef9e3ee1fa9..9a649c50716 100644 --- a/includes/class-wc-payments-checkout.php +++ b/includes/class-wc-payments-checkout.php @@ -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 @@ -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'; @@ -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. ?> gateway->get_description() ) ) : ?> diff --git a/includes/fraud-prevention/class-fraud-prevention-service.php b/includes/fraud-prevention/class-fraud-prevention-service.php index ea351d1a326..abab5b9f454 100644 --- a/includes/fraud-prevention/class-fraud-prevention-service.php +++ b/includes/fraud-prevention/class-fraud-prevention-service.php @@ -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;