Skip to content

Commit ea3c0d4

Browse files
elvismdevwjrosamalithsen
authored
Fix Express Checkout buttons not appearing on order-pay pages due to script timing issue (#4446)
* Fix: Express Checkout buttons not appearing on order-pay pages. * Refactor: Extract duplicated asset loading logic into reusable method --------- Co-authored-by: Wesley Rosa <[email protected]> Co-authored-by: Malith Senaweera <[email protected]>
1 parent 65b61f7 commit ea3c0d4

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

includes/payment-methods/class-wc-stripe-express-checkout-element.php

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,10 @@ function ( $field ) {
466466
* @param WC_Order $order The order that needs payment.
467467
*/
468468
public function localize_pay_for_order_page_scripts( $order ) {
469+
// Ensure the script is registered before localizing
470+
if ( ! wp_script_is( 'wc_stripe_express_checkout', 'registered' ) ) {
471+
$this->register_express_checkout_script();
472+
}
469473
$currency = get_woocommerce_currency();
470474
$data = [];
471475
$items = [];
@@ -553,21 +557,15 @@ public function localize_pay_for_order_page_scripts( $order ) {
553557
}
554558

555559
/**
556-
* Load scripts and styles.
560+
* Get asset file data (version and dependencies).
561+
*
562+
* @return array Array containing 'version' and 'dependencies' keys.
557563
*/
558-
public function scripts() {
559-
// If page is not supported, bail.
560-
if ( ! $this->express_checkout_helper->is_page_supported() ) {
561-
return;
562-
}
563-
564-
if ( ! $this->express_checkout_helper->should_show_express_checkout_button() ) {
565-
return;
566-
}
567-
564+
private function get_asset_data() {
568565
$asset_path = WC_STRIPE_PLUGIN_PATH . '/build/express-checkout.asset.php';
569566
$version = WC_STRIPE_VERSION;
570567
$dependencies = [];
568+
571569
if ( file_exists( $asset_path ) ) {
572570
$asset = require $asset_path;
573571
$version = is_array( $asset ) && isset( $asset['version'] )
@@ -578,20 +576,53 @@ public function scripts() {
578576
: $dependencies;
579577
}
580578

579+
return [
580+
'version' => $version,
581+
'dependencies' => $dependencies,
582+
];
583+
}
584+
585+
/**
586+
* Register the express checkout script without enqueuing it.
587+
*/
588+
private function register_express_checkout_script() {
589+
$asset_data = $this->get_asset_data();
590+
581591
wp_register_script( 'stripe', 'https://js.stripe.com/v3/', '', '3.0', true );
582592
wp_register_script(
583593
'wc_stripe_express_checkout',
584594
WC_STRIPE_PLUGIN_URL . '/build/express-checkout.js',
585-
array_merge( [ 'jquery', 'stripe' ], $dependencies ),
586-
$version,
595+
array_merge( [ 'jquery', 'stripe' ], $asset_data['dependencies'] ),
596+
$asset_data['version'],
587597
true
588598
);
599+
}
600+
601+
/**
602+
* Load scripts and styles.
603+
*/
604+
public function scripts() {
605+
// If page is not supported, bail.
606+
if ( ! $this->express_checkout_helper->is_page_supported() ) {
607+
return;
608+
}
609+
610+
if ( ! $this->express_checkout_helper->should_show_express_checkout_button() ) {
611+
return;
612+
}
613+
614+
// Register the script if not already registered
615+
if ( ! wp_script_is( 'wc_stripe_express_checkout', 'registered' ) ) {
616+
$this->register_express_checkout_script();
617+
}
618+
619+
$asset_data = $this->get_asset_data();
589620

590621
wp_enqueue_style(
591622
'wc_stripe_express_checkout_style',
592623
WC_STRIPE_PLUGIN_URL . '/build/express-checkout.css',
593624
[],
594-
$version
625+
$asset_data['version']
595626
);
596627

597628
wp_localize_script(

0 commit comments

Comments
 (0)