|
99 | 99 | setButtonIcons( active ); |
100 | 100 | } |
101 | 101 |
|
| 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 | + |
102 | 124 | /** |
103 | 125 | * Re-apply icon after Vue re-renders the button (e.g. spinner swap). |
104 | 126 | * The CSS survives re-renders; only the icon DOM node needs re-injection. |
105 | 127 | */ |
106 | 128 | ( function watchForRerender() { |
107 | | - let isPayPalActive = false; |
| 129 | + let isPayPalSelected = false; |
108 | 130 |
|
109 | 131 | const observer = new MutationObserver( function () { |
110 | | - if ( isPayPalActive ) { |
| 132 | + if ( isPayPalSelected && orderRequiresPayment() ) { |
111 | 133 | setButtonIcons( true ); |
112 | 134 | } |
113 | 135 | } ); |
114 | 136 |
|
| 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 | + |
115 | 157 | wp.hooks.addAction( |
116 | 158 | 'wu_on_change_gateway', |
117 | 159 | 'wp-ultimo/paypal-rest', |
118 | 160 | 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(); |
130 | 175 | } |
131 | 176 | ); |
132 | 177 | }() ); |
|
0 commit comments