Skip to content

Commit ecedf74

Browse files
authored
Merge main into bugfix/stuck-publishing-timeout
2 parents 3c07e3b + a4bb4df commit ecedf74

7 files changed

Lines changed: 1386 additions & 26 deletions

File tree

assets/js/gateways/paypal-rest.js

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,34 +99,79 @@
9999
setButtonIcons( active );
100100
}
101101

102+
/**
103+
* Check whether the current order actually requires payment.
104+
*
105+
* When the cart total is zero (free plan, 100 % discount, etc.)
106+
* the checkout hides the payment-method selector and the server
107+
* will route the signup through the free gateway — so the submit
108+
* button should NOT carry PayPal branding.
109+
*
110+
* @return {boolean} True when the order needs payment collection.
111+
*/
112+
function orderRequiresPayment() {
113+
const checkout = window.wu_checkout_form;
114+
115+
if ( ! checkout || ! checkout.order ) {
116+
// Order not loaded yet — assume payment is needed so branding
117+
// can be applied once the order confirms it.
118+
return true;
119+
}
120+
121+
return !! checkout.order.should_collect_payment;
122+
}
123+
102124
/**
103125
* Re-apply icon after Vue re-renders the button (e.g. spinner swap).
104126
* The CSS survives re-renders; only the icon DOM node needs re-injection.
105127
*/
106128
( function watchForRerender() {
107-
let isPayPalActive = false;
129+
let isPayPalSelected = false;
108130

109131
const observer = new MutationObserver( function () {
110-
if ( isPayPalActive ) {
132+
if ( isPayPalSelected && orderRequiresPayment() ) {
111133
setButtonIcons( true );
112134
}
113135
} );
114136

137+
/**
138+
* Recalculate whether branding should be shown.
139+
*
140+
* Branding is active only when PayPal is the selected gateway
141+
* AND the order actually requires payment collection.
142+
*/
143+
function refreshBranding() {
144+
const shouldBrand = isPayPalSelected && orderRequiresPayment();
145+
applyBranding( shouldBrand );
146+
147+
const form = document.getElementById( 'wu_form' );
148+
if ( form ) {
149+
if ( shouldBrand ) {
150+
observer.observe( form, { childList: true, subtree: true } );
151+
} else {
152+
observer.disconnect();
153+
}
154+
}
155+
}
156+
115157
wp.hooks.addAction(
116158
'wu_on_change_gateway',
117159
'wp-ultimo/paypal-rest',
118160
function ( newGateway ) {
119-
isPayPalActive = ( newGateway === GATEWAY_ID );
120-
applyBranding( isPayPalActive );
121-
122-
const form = document.getElementById( 'wu_form' );
123-
if ( form ) {
124-
if ( isPayPalActive ) {
125-
observer.observe( form, { childList: true, subtree: true } );
126-
} else {
127-
observer.disconnect();
128-
}
129-
}
161+
isPayPalSelected = ( newGateway === GATEWAY_ID );
162+
refreshBranding();
163+
}
164+
);
165+
166+
/*
167+
* When the order updates (product change, coupon applied, etc.)
168+
* the should_collect_payment flag may flip — re-evaluate branding.
169+
*/
170+
wp.hooks.addAction(
171+
'wu_on_form_updated',
172+
'wp-ultimo/paypal-rest',
173+
function () {
174+
refreshBranding();
130175
}
131176
);
132177
}() );

0 commit comments

Comments
 (0)