| @@ -29,11 +29,19 @@ | ||
| 29 | 29 | { |
| 30 | 30 | public static function register() |
| 31 | 31 | { |
| 32 | 32 | |
| 33 | + // Late on init, deliberately. These routes do not merely register — they | |
| 34 | + // TAKE OVER the request, render a full page and die(). At the default | |
| 35 | + // priority this callback is queued at plugin-include time, so it runs | |
| 36 | + // before anything that registers on init from `fluentcart_loaded` (every | |
| 37 | + // add-on, including FluentCart Pro). A route that renders and dies before | |
| 38 | + // those listeners exist silently drops whatever they would have rendered | |
| 39 | + // — which is why the saved-payment-method picker and the save-my-card | |
| 40 | + // consent box appeared on the checkout page but never in modal checkout. | |
| 33 | 41 | add_action('init', function () { |
| 34 | 42 | self::registerRoutes(); |
| 35 | - }); | |
| 43 | + }, 99); | |
| 36 | 44 | } |
| 37 | 45 | |
| 38 | 46 | public static function renderModalCheckout() { |
| 39 | 47 | add_action('wp_footer', function () { |
| @@ -143,9 +151,35 @@ | ||
| 143 | 151 | $coupons = App::request()->get('coupons', ''); |
| 144 | 152 | if ($coupons) { |
| 145 | 153 | $coupons = explode(',', $coupons); |
| 146 | 154 | $coupons = array_map('sanitize_text_field', $coupons); |
| 147 | - $cart->applyCoupon($coupons); | |
| 155 | + $couponResult = $cart->applyCoupon($coupons); | |
| 156 | + | |
| 157 | + | |
| 158 | + $couponErrors = []; | |
| 159 | + if (is_wp_error($couponResult)) { | |
| 160 | + $couponErrors[] = esc_html($couponResult->get_error_message()); | |
| 161 | + } elseif (is_array($couponResult)) { | |
| 162 | + $perCouponResults = Arr::get($couponResult, 'coupon_results', []); | |
| 163 | + foreach ($coupons as $code) { | |
| 164 | + foreach ($perCouponResults as $resultCode => $result) { | |
| 165 | + if (strcasecmp((string) $resultCode, (string) $code) === 0) { | |
| 166 | + $errorMessage = Arr::get($result, 'error', ''); | |
| 167 | + if ($errorMessage !== '') { | |
| 168 | + $couponErrors[] = esc_html($errorMessage); | |
| 169 | + } | |
| 170 | + break; | |
| 171 | + } | |
| 172 | + } | |
| 173 | + } | |
| 174 | + } | |
| 175 | + | |
| 176 | + if ($couponErrors) { | |
| 177 | + $checkoutData = is_array($cart->checkout_data) ? $cart->checkout_data : []; | |
| 178 | + $checkoutData['__checkout_error_notices'] = $couponErrors; | |
| 179 | + $cart->checkout_data = $checkoutData; | |
| 180 | + $cart->save(); | |
| 181 | + } | |
| 148 | 182 | } |
| 149 | 183 | |
| 150 | 184 | $target_path = (new StoreSettings())->getCheckoutPage(); |
| 151 | 185 | |