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

Load checkout scripts when they are not previously loaded on checkout page #9984

Merged
merged 3 commits into from
Dec 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/load-checkout-scripts-on-checkout-if-not-loaded
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Load checkout scripts when they are not previously loaded on checkout page.
31 changes: 25 additions & 6 deletions includes/class-wc-payments-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function init_hooks() {

add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts_for_zero_order_total' ], 11 );
add_action( 'woocommerce_after_checkout_form', [ $this, 'maybe_load_checkout_scripts' ] );
}

/**
Expand Down Expand Up @@ -151,11 +152,18 @@ public function register_scripts_for_zero_order_total() {
! has_block( 'woocommerce/checkout' ) &&
! wp_script_is( 'wcpay-upe-checkout', 'enqueued' )
) {
WC_Payments::get_gateway()->tokenization_script();
$script_handle = 'wcpay-upe-checkout';
$js_object = 'wcpay_upe_config';
wp_localize_script( $script_handle, $js_object, WC_Payments::get_wc_payments_checkout()->get_payment_fields_js_config() );
wp_enqueue_script( $script_handle );
$this->load_checkout_scripts();
}
}

/**
* Sometimes the filters can remove the payment gateway from the checkout page which results in the payment fields not being displayed.
* This could prevent loading of the payment fields (checkout) scripts.
* This function ensures that these scripts are loaded.
*/
public function maybe_load_checkout_scripts() {
if ( is_checkout() && ! wp_script_is( 'wcpay-upe-checkout', 'enqueued' ) ) {
$this->load_checkout_scripts();
}
}

Expand Down Expand Up @@ -416,7 +424,7 @@ function () use ( $prepared_customer_data ) {
}

?>
<div class="wcpay-upe-form"
<div class="wcpay-upe-form"
data-payment-method-type="<?php echo esc_attr( $this->gateway->get_stripe_id() ); ?>"
>
<?php
Expand Down Expand Up @@ -497,4 +505,15 @@ public function set_gateway( $payment_method_id ) {
$this->gateway = $this->gateway->wc_payments_get_payment_gateway_by_id( $payment_method_id );
}
}

/**
* Load the checkout scripts.
*/
private function load_checkout_scripts() {
WC_Payments::get_gateway()->tokenization_script();
$script_handle = 'wcpay-upe-checkout';
$js_object = 'wcpay_upe_config';
wp_localize_script( $script_handle, $js_object, WC_Payments::get_wc_payments_checkout()->get_payment_fields_js_config() );
wp_enqueue_script( $script_handle );
}
}
Loading