| 1 |
// Custom initialization for default widgets IOK 2025-05-13 |
| 2 |
jQuery('body').on('woo-vipps-checkout-widgets-loaded', function () { |
| 3 |
if (jQuery('#vipps_checkout_widget_coupon_active_codes_container_codes').children().length > 0) { |
| 4 |
jQuery('#vipps_checkout_widget_coupon_active_codes_container').show(); |
| 5 |
} |
| 6 |
|
| 7 |
// Coupon code widget submit. LP 2025-05-08 |
| 8 |
jQuery('#vipps_checkout_widget_coupon_form').on('submit', function(e) { |
| 9 |
e.preventDefault(); |
| 10 |
const formdata = new FormData(this); |
| 11 |
let code = formdata.get('code').trim(); |
| 12 |
|
| 13 |
const error = jQuery('#vipps_checkout_widget_coupon_error'); |
| 14 |
const success = jQuery('#vipps_checkout_widget_coupon_success'); |
| 15 |
const input = jQuery('#vipps_checkout_widget_coupon_code'); |
| 16 |
const activeCodesContainer = jQuery('#vipps_checkout_widget_coupon_active_codes_container'); |
| 17 |
const activeCodes = jQuery('#vipps_checkout_widget_coupon_active_codes_container_codes'); |
| 18 |
function showError() { |
| 19 |
error.show(); |
| 20 |
success.hide(); |
| 21 |
if (!input.hasClass('error')) input.addClass('error'); |
| 22 |
if (input.hasClass('success')) input.removeClass('success'); |
| 23 |
} |
| 24 |
|
| 25 |
function showSuccess() { |
| 26 |
error.hide(); |
| 27 |
success.show(); |
| 28 |
if (input.hasClass('error')) input.removeClass('error'); |
| 29 |
if (!input.hasClass('success')) input.addClass('success'); |
| 30 |
input.val('').blur(); |
| 31 |
const newCode = jQuery(` |
| 32 |
<div class="vipps_checkout_widget_coupon_active_code_box" id="vipps_checkout_widget_coupon_active_code_${code}"> |
| 33 |
<span class="vipps_checkout_widget_coupon_active_code">${code}</span> |
| 34 |
</div> |
| 35 |
`); |
| 36 |
const newCodeDelete = jQuery('<span class="vipps_checkout_widget_coupon_delete">✕</span>'); |
| 37 |
newCodeDelete.on('click', deleteActiveCouponCode); |
| 38 |
newCode.append(newCodeDelete); |
| 39 |
activeCodes.append(newCode); |
| 40 |
activeCodesContainer.show(); |
| 41 |
} |
| 42 |
|
| 43 |
if (code) { |
| 44 |
const args = { lock_held: true, data: { 'code': code } }; |
| 45 |
args['error'] = function (result) { |
| 46 |
console.log("Problem: " + result['error']); |
| 47 |
showError(); |
| 48 |
} |
| 49 |
args['success'] = function (result) { |
| 50 |
console.log("Success: %j", result); |
| 51 |
if (result['msg'] && result['msg'] == 1) { |
| 52 |
showSuccess(); |
| 53 |
return; |
| 54 |
} |
| 55 |
showError(); |
| 56 |
} |
| 57 |
|
| 58 |
wooVippsCheckoutCallback( 'submitcoupon', args ); |
| 59 |
} |
| 60 |
}); |
| 61 |
|
| 62 |
// Coupon code widget remove active coupon code. LP 2025-05-09 |
| 63 |
function deleteActiveCouponCode() { |
| 64 |
const el = jQuery(this); |
| 65 |
const code = el.prev().text(); |
| 66 |
|
| 67 |
const error = jQuery('#vipps_checkout_widget_coupon_error'); |
| 68 |
const success = jQuery('#vipps_checkout_widget_coupon_success'); |
| 69 |
const input = jQuery('#vipps_checkout_widget_coupon_code'); |
| 70 |
const deleteError = jQuery('#vipps_checkout_widget_coupon_delete_error'); |
| 71 |
function showError() { |
| 72 |
success.hide(); |
| 73 |
error.hide(); |
| 74 |
deleteError.show(); |
| 75 |
if (input.hasClass('error')) input.removeClass('error'); |
| 76 |
if (input.hasClass('success')) input.removeClass('success'); |
| 77 |
} |
| 78 |
|
| 79 |
function showSuccess() { |
| 80 |
el.parent().remove(); |
| 81 |
success.hide(); |
| 82 |
error.hide(); |
| 83 |
deleteError.hide(); |
| 84 |
if (jQuery('#vipps_checkout_widget_coupon_active_codes_container_codes').children().length < 1) { |
| 85 |
jQuery('#vipps_checkout_widget_coupon_active_codes_container').hide(); |
| 86 |
} |
| 87 |
if (input.hasClass('error')) input.removeClass('error'); |
| 88 |
if (input.hasClass('success')) input.removeClass('success'); |
| 89 |
} |
| 90 |
|
| 91 |
if (code) { |
| 92 |
const args = { lock_held: true, data: { 'code': code } }; |
| 93 |
args['error'] = function (result) { |
| 94 |
console.log("Problem: " + result['error']); |
| 95 |
showError(); |
| 96 |
} |
| 97 |
args['success'] = function (result) { |
| 98 |
console.log("Success: %j", result); |
| 99 |
if (result['msg'] && result['msg'] == 1) { |
| 100 |
showSuccess(); |
| 101 |
return; |
| 102 |
} |
| 103 |
showError(); |
| 104 |
} |
| 105 |
|
| 106 |
wooVippsCheckoutCallback( 'removecoupon', args ); |
| 107 |
} |
| 108 |
}; |
| 109 |
jQuery('.vipps_checkout_widget_coupon_delete').on('click', deleteActiveCouponCode); |
| 110 |
|
| 111 |
// Order notes widget submit. LP 2025-05-12 |
| 112 |
jQuery('#vipps_checkout_widget_ordernotes_form').on('submit', function(e) { |
| 113 |
e.preventDefault(); |
| 114 |
const formdata = new FormData(this); |
| 115 |
let notes = formdata.get('notes').trim(); |
| 116 |
|
| 117 |
const error = jQuery('#vipps_checkout_widget_ordernotes_error'); |
| 118 |
const success = jQuery('#vipps_checkout_widget_ordernotes_success'); |
| 119 |
const input = jQuery('#vipps_checkout_widget_ordernotes_input'); |
| 120 |
|
| 121 |
const args = { lock_held: true, data: { 'notes': notes } }; |
| 122 |
args['error'] = function (result) { |
| 123 |
console.log("Problem: " + result['error']); |
| 124 |
error.show(); |
| 125 |
success.hide(); |
| 126 |
if (!input.hasClass('error')) input.addClass('error'); |
| 127 |
if (input.hasClass('success')) input.removeClass('success'); |
| 128 |
} |
| 129 |
args['success'] = function (result) { |
| 130 |
console.log("Success: %j", result); |
| 131 |
error.hide(); |
| 132 |
success.show(); |
| 133 |
if (input.hasClass('error')) input.removeClass('error'); |
| 134 |
if (!input.hasClass('success')) input.addClass('success'); |
| 135 |
} |
| 136 |
wooVippsCheckoutCallback( "submitnotes", args ); |
| 137 |
}); |
| 138 |
|
| 139 |
jQuery('#vipps_checkout_widget_ordernotes_input').on('input', function() { |
| 140 |
const success = jQuery('#vipps_checkout_widget_ordernotes_success'); |
| 141 |
const input = jQuery('#vipps_checkout_widget_ordernotes_input'); |
| 142 |
const error = jQuery('#vipps_checkout_widget_ordernotes_error'); |
| 143 |
if (input.hasClass('error')) input.removeClass('error'); |
| 144 |
if (input.hasClass('success')) input.removeClass('success'); |
| 145 |
success.hide(); |
| 146 |
error.hide(); |
| 147 |
}); |
| 148 |
|
| 149 |
}); |
| 150 |
|
| 151 |
|