|
900 | 900 | prefix + " " + urmf_data.labels.i18n_coupon_discount_message |
901 | 901 | ); |
902 | 902 | }, |
| 903 | + /** |
| 904 | + * Clear an applied coupon and recalculate totals. |
| 905 | + * Used by the cancel (X) control and when the coupon input is emptied. |
| 906 | + * |
| 907 | + * @param {Object} options Optional settings. |
| 908 | + * @param {boolean} options.skipEmptyInput Skip clearing the input when already empty. |
| 909 | + */ |
| 910 | + clear_applied_coupon: function (options) { |
| 911 | + options = options || {}; |
| 912 | + |
| 913 | + if (!options.skipEmptyInput) { |
| 914 | + $("#ur-membership-coupon").val(""); |
| 915 | + } |
| 916 | + |
| 917 | + $("#coupon-validation-error") |
| 918 | + .text("") |
| 919 | + .removeClass("notice_blue notice_red"); |
| 920 | + $(".urm_apply_coupon").show(); |
| 921 | + $("#total-input-notice").text(""); |
| 922 | + |
| 923 | + var selected_membership = $( |
| 924 | + 'input[name="urm_membership"]:checked' |
| 925 | + ); |
| 926 | + |
| 927 | + selected_membership |
| 928 | + .removeData("ur-discount-amount") |
| 929 | + .removeAttr("data-ur-discount-amount"); |
| 930 | + |
| 931 | + if (selected_membership.length) { |
| 932 | + ur_membership_ajax_utils.calculate_total(selected_membership); |
| 933 | + } |
| 934 | + }, |
903 | 935 | /** |
904 | 936 | * Send data to the backend API. |
905 | 937 | * |
|
3360 | 3392 | }); |
3361 | 3393 | //coupon clear input |
3362 | 3394 | $(document).on("click", ".ur_clear_coupon", function () { |
3363 | | - $("#ur-membership-coupon").val(""); |
3364 | | - $("#coupon-validation-error").text(""); |
3365 | | - $(".urm_apply_coupon").show(); |
3366 | | - $("#total-input-notice").text(""); |
| 3395 | + ur_membership_ajax_utils.clear_applied_coupon(); |
| 3396 | + }); |
| 3397 | + // Clear applied coupon when the field is emptied (e.g. backspace), same as cancel (X). |
| 3398 | + $(document).on("input", "#ur-membership-coupon", function () { |
| 3399 | + if ($(this).val().trim() !== "") { |
| 3400 | + return; |
| 3401 | + } |
| 3402 | + |
3367 | 3403 | var selected_membership = $( |
3368 | 3404 | 'input[name="urm_membership"]:checked' |
3369 | 3405 | ); |
3370 | | - selected_membership |
3371 | | - .removeData("ur-discount-amount") |
3372 | | - .removeAttr("data-ur-discount-amount"); |
3373 | | - ur_membership_ajax_utils.calculate_total(selected_membership); |
| 3406 | + var hasAppliedDiscount = |
| 3407 | + typeof selected_membership.attr( |
| 3408 | + "data-ur-discount-amount" |
| 3409 | + ) !== "undefined" || |
| 3410 | + typeof selected_membership.data("ur-discount-amount") !== |
| 3411 | + "undefined"; |
| 3412 | + |
| 3413 | + if (!hasAppliedDiscount) { |
| 3414 | + $("#coupon-validation-error") |
| 3415 | + .text("") |
| 3416 | + .removeClass("notice_blue notice_red"); |
| 3417 | + $(".urm_apply_coupon").show(); |
| 3418 | + return; |
| 3419 | + } |
| 3420 | + |
| 3421 | + ur_membership_ajax_utils.clear_applied_coupon({ |
| 3422 | + skipEmptyInput: true |
| 3423 | + }); |
3374 | 3424 | }); |
3375 | 3425 | //redirect to membership member registration form |
3376 | 3426 | $(document).on( |
|
3985 | 4035 | var oldText = $periodText.length |
3986 | 4036 | ? $periodText.text() |
3987 | 4037 | : $span.text(); |
3988 | | - var parts = oldText.split("/"); |
3989 | | - var durationPart = parts[1] |
3990 | | - ? "/ " + parts[1].trim() |
3991 | | - : ""; |
| 4038 | + var durationPart = ""; |
| 4039 | + var everyIndex = oldText |
| 4040 | + .toLowerCase() |
| 4041 | + .indexOf(" every "); |
| 4042 | + |
| 4043 | + if (everyIndex !== -1) { |
| 4044 | + durationPart = oldText |
| 4045 | + .substring(everyIndex) |
| 4046 | + .trim(); |
| 4047 | + } else { |
| 4048 | + // Legacy slash format: "$200.00 / 2 Weeks". |
| 4049 | + var parts = oldText.split("/"); |
| 4050 | + durationPart = parts[1] |
| 4051 | + ? "/ " + parts[1].trim() |
| 4052 | + : ""; |
| 4053 | + } |
3992 | 4054 |
|
3993 | 4055 | if (localCurrencyDetails[currency]) { |
3994 | 4056 | var newCalculatedValue = |
|
0 commit comments