Skip to content

Commit

Permalink
Fix duplicated enqueued scripts in shortcode checkout (#8954)
Browse files Browse the repository at this point in the history
Co-authored-by: Rafael Zaleski <rafaelzaleski@users.noreply.github.com>
  • Loading branch information
ricardo and rafaelzaleski committed Jun 18, 2024
1 parent 549100e commit fa75540
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
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

0 comments on commit fa75540

Please sign in to comment.