checkout.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /* global wc_checkout_params */
  2. jQuery( function( $ ) {
  3. // wc_checkout_params is required to continue, ensure the object exists
  4. if ( typeof wc_checkout_params === 'undefined' ) {
  5. return false;
  6. }
  7. $.blockUI.defaults.overlayCSS.cursor = 'default';
  8. var wc_checkout_form = {
  9. updateTimer: false,
  10. dirtyInput: false,
  11. selectedPaymentMethod: false,
  12. xhr: false,
  13. $order_review: $( '#order_review' ),
  14. $checkout_form: $( 'form.checkout' ),
  15. init: function() {
  16. $( document.body ).bind( 'update_checkout', this.update_checkout );
  17. $( document.body ).bind( 'init_checkout', this.init_checkout );
  18. // Payment methods
  19. this.$checkout_form.on( 'click', 'input[name="payment_method"]', this.payment_method_selected );
  20. if ( $( document.body ).hasClass( 'woocommerce-order-pay' ) ) {
  21. this.$order_review.on( 'click', 'input[name="payment_method"]', this.payment_method_selected );
  22. }
  23. // Prevent HTML5 validation which can conflict.
  24. this.$checkout_form.attr( 'novalidate', 'novalidate' );
  25. // Form submission
  26. this.$checkout_form.on( 'submit', this.submit );
  27. // Inline validation
  28. this.$checkout_form.on( 'input validate change', '.input-text, select, input:checkbox', this.validate_field );
  29. // Manual trigger
  30. this.$checkout_form.on( 'update', this.trigger_update_checkout );
  31. // Inputs/selects which update totals
  32. this.$checkout_form.on( 'change', 'select.shipping_method, input[name^="shipping_method"], #ship-to-different-address input, .update_totals_on_change select, .update_totals_on_change input[type="radio"], .update_totals_on_change input[type="checkbox"]', this.trigger_update_checkout );
  33. this.$checkout_form.on( 'change', '.address-field select', this.input_changed );
  34. this.$checkout_form.on( 'change', '.address-field input.input-text, .update_totals_on_change input.input-text', this.maybe_input_changed );
  35. this.$checkout_form.on( 'keydown', '.address-field input.input-text, .update_totals_on_change input.input-text', this.queue_update_checkout );
  36. // Address fields
  37. this.$checkout_form.on( 'change', '#ship-to-different-address input', this.ship_to_different_address );
  38. // Trigger events
  39. this.$checkout_form.find( '#ship-to-different-address input' ).change();
  40. this.init_payment_methods();
  41. // Update on page load
  42. if ( wc_checkout_params.is_checkout === '1' ) {
  43. $( document.body ).trigger( 'init_checkout' );
  44. }
  45. if ( wc_checkout_params.option_guest_checkout === 'yes' ) {
  46. $( 'input#createaccount' ).change( this.toggle_create_account ).change();
  47. }
  48. },
  49. init_payment_methods: function() {
  50. var $payment_methods = $( '.woocommerce-checkout' ).find( 'input[name="payment_method"]' );
  51. // If there is one method, we can hide the radio input
  52. if ( 1 === $payment_methods.length ) {
  53. $payment_methods.eq(0).hide();
  54. }
  55. // If there was a previously selected method, check that one.
  56. if ( wc_checkout_form.selectedPaymentMethod ) {
  57. $( '#' + wc_checkout_form.selectedPaymentMethod ).prop( 'checked', true );
  58. }
  59. // If there are none selected, select the first.
  60. if ( 0 === $payment_methods.filter( ':checked' ).length ) {
  61. $payment_methods.eq(0).prop( 'checked', true );
  62. }
  63. if ( $payment_methods.length > 1 ) {
  64. // Hide open descriptions.
  65. $( 'div.payment_box' ).filter( ':visible' ).slideUp( 0 );
  66. }
  67. // Trigger click event for selected method
  68. $payment_methods.filter( ':checked' ).eq(0).trigger( 'click' );
  69. },
  70. get_payment_method: function() {
  71. return wc_checkout_form.$checkout_form.find( 'input[name="payment_method"]:checked' ).val();
  72. },
  73. payment_method_selected: function( e ) {
  74. e.stopPropagation();
  75. if ( $( '.payment_methods input.input-radio' ).length > 1 ) {
  76. var target_payment_box = $( 'div.payment_box.' + $( this ).attr( 'ID' ) ),
  77. is_checked = $( this ).is( ':checked' );
  78. if ( is_checked && ! target_payment_box.is( ':visible' ) ) {
  79. $( 'div.payment_box' ).filter( ':visible' ).slideUp( 230 );
  80. if ( is_checked ) {
  81. target_payment_box.slideDown( 230 );
  82. }
  83. }
  84. } else {
  85. $( 'div.payment_box' ).show();
  86. }
  87. if ( $( this ).data( 'order_button_text' ) ) {
  88. $( '#place_order' ).text( $( this ).data( 'order_button_text' ) );
  89. } else {
  90. $( '#place_order' ).text( $( '#place_order' ).data( 'value' ) );
  91. }
  92. var selectedPaymentMethod = $( '.woocommerce-checkout input[name="payment_method"]:checked' ).attr( 'id' );
  93. if ( selectedPaymentMethod !== wc_checkout_form.selectedPaymentMethod ) {
  94. $( document.body ).trigger( 'payment_method_selected' );
  95. }
  96. wc_checkout_form.selectedPaymentMethod = selectedPaymentMethod;
  97. },
  98. toggle_create_account: function() {
  99. $( 'div.create-account' ).hide();
  100. if ( $( this ).is( ':checked' ) ) {
  101. // Ensure password is not pre-populated.
  102. $( '#account_password' ).val( '' ).change();
  103. $( 'div.create-account' ).slideDown();
  104. }
  105. },
  106. init_checkout: function() {
  107. $( '#billing_country, #shipping_country, .country_to_state' ).change();
  108. $( document.body ).trigger( 'update_checkout' );
  109. },
  110. maybe_input_changed: function( e ) {
  111. if ( wc_checkout_form.dirtyInput ) {
  112. wc_checkout_form.input_changed( e );
  113. }
  114. },
  115. input_changed: function( e ) {
  116. wc_checkout_form.dirtyInput = e.target;
  117. wc_checkout_form.maybe_update_checkout();
  118. },
  119. queue_update_checkout: function( e ) {
  120. var code = e.keyCode || e.which || 0;
  121. if ( code === 9 ) {
  122. return true;
  123. }
  124. wc_checkout_form.dirtyInput = this;
  125. wc_checkout_form.reset_update_checkout_timer();
  126. wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.maybe_update_checkout, '1000' );
  127. },
  128. trigger_update_checkout: function() {
  129. wc_checkout_form.reset_update_checkout_timer();
  130. wc_checkout_form.dirtyInput = false;
  131. $( document.body ).trigger( 'update_checkout' );
  132. },
  133. maybe_update_checkout: function() {
  134. var update_totals = true;
  135. if ( $( wc_checkout_form.dirtyInput ).length ) {
  136. var $required_inputs = $( wc_checkout_form.dirtyInput ).closest( 'div' ).find( '.address-field.validate-required' );
  137. if ( $required_inputs.length ) {
  138. $required_inputs.each( function() {
  139. if ( $( this ).find( 'input.input-text' ).val() === '' ) {
  140. update_totals = false;
  141. }
  142. });
  143. }
  144. }
  145. if ( update_totals ) {
  146. wc_checkout_form.trigger_update_checkout();
  147. }
  148. },
  149. ship_to_different_address: function() {
  150. $( 'div.shipping_address' ).hide();
  151. if ( $( this ).is( ':checked' ) ) {
  152. $( 'div.shipping_address' ).slideDown();
  153. }
  154. },
  155. reset_update_checkout_timer: function() {
  156. clearTimeout( wc_checkout_form.updateTimer );
  157. },
  158. is_valid_json: function( raw_json ) {
  159. try {
  160. var json = $.parseJSON( raw_json );
  161. return ( json && 'object' === typeof json );
  162. } catch ( e ) {
  163. return false;
  164. }
  165. },
  166. validate_field: function( e ) {
  167. var $this = $( this ),
  168. $parent = $this.closest( '.form-row' ),
  169. validated = true,
  170. validate_required = $parent.is( '.validate-required' ),
  171. validate_email = $parent.is( '.validate-email' ),
  172. event_type = e.type;
  173. if ( 'input' === event_type ) {
  174. $parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email woocommerce-validated' );
  175. }
  176. if ( 'validate' === event_type || 'change' === event_type ) {
  177. if ( validate_required ) {
  178. if ( 'checkbox' === $this.attr( 'type' ) && ! $this.is( ':checked' ) ) {
  179. $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' );
  180. validated = false;
  181. } else if ( $this.val() === '' ) {
  182. $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' );
  183. validated = false;
  184. }
  185. }
  186. if ( validate_email ) {
  187. if ( $this.val() ) {
  188. /* https://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex */
  189. var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
  190. if ( ! pattern.test( $this.val() ) ) {
  191. $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-email' );
  192. validated = false;
  193. }
  194. }
  195. }
  196. if ( validated ) {
  197. $parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email' ).addClass( 'woocommerce-validated' );
  198. }
  199. }
  200. },
  201. update_checkout: function( event, args ) {
  202. // Small timeout to prevent multiple requests when several fields update at the same time
  203. wc_checkout_form.reset_update_checkout_timer();
  204. wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.update_checkout_action, '5', args );
  205. },
  206. update_checkout_action: function( args ) {
  207. if ( wc_checkout_form.xhr ) {
  208. wc_checkout_form.xhr.abort();
  209. }
  210. if ( $( 'form.checkout' ).length === 0 ) {
  211. return;
  212. }
  213. args = typeof args !== 'undefined' ? args : {
  214. update_shipping_method: true
  215. };
  216. var country = $( '#billing_country' ).val(),
  217. state = $( '#billing_state' ).val(),
  218. postcode = $( 'input#billing_postcode' ).val(),
  219. city = $( '#billing_city' ).val(),
  220. address = $( 'input#billing_address_1' ).val(),
  221. address_2 = $( 'input#billing_address_2' ).val(),
  222. s_country = country,
  223. s_state = state,
  224. s_postcode = postcode,
  225. s_city = city,
  226. s_address = address,
  227. s_address_2 = address_2,
  228. $required_inputs = $( wc_checkout_form.$checkout_form ).find( '.address-field.validate-required:visible' ),
  229. has_full_address = true;
  230. if ( $required_inputs.length ) {
  231. $required_inputs.each( function() {
  232. if ( $( this ).find( ':input' ).val() === '' ) {
  233. has_full_address = false;
  234. }
  235. });
  236. }
  237. if ( $( '#ship-to-different-address' ).find( 'input' ).is( ':checked' ) ) {
  238. s_country = $( '#shipping_country' ).val();
  239. s_state = $( '#shipping_state' ).val();
  240. s_postcode = $( 'input#shipping_postcode' ).val();
  241. s_city = $( '#shipping_city' ).val();
  242. s_address = $( 'input#shipping_address_1' ).val();
  243. s_address_2 = $( 'input#shipping_address_2' ).val();
  244. }
  245. var data = {
  246. security : wc_checkout_params.update_order_review_nonce,
  247. payment_method : wc_checkout_form.get_payment_method(),
  248. country : country,
  249. state : state,
  250. postcode : postcode,
  251. city : city,
  252. address : address,
  253. address_2 : address_2,
  254. s_country : s_country,
  255. s_state : s_state,
  256. s_postcode : s_postcode,
  257. s_city : s_city,
  258. s_address : s_address,
  259. s_address_2 : s_address_2,
  260. has_full_address: has_full_address,
  261. post_data : $( 'form.checkout' ).serialize()
  262. };
  263. if ( false !== args.update_shipping_method ) {
  264. var shipping_methods = {};
  265. $( 'select.shipping_method, input[name^="shipping_method"][type="radio"]:checked, input[name^="shipping_method"][type="hidden"]' ).each( function() {
  266. shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val();
  267. } );
  268. data.shipping_method = shipping_methods;
  269. }
  270. $( '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' ).block({
  271. message: null,
  272. overlayCSS: {
  273. background: '#fff',
  274. opacity: 0.6
  275. }
  276. });
  277. wc_checkout_form.xhr = $.ajax({
  278. type: 'POST',
  279. url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'update_order_review' ),
  280. data: data,
  281. success: function( data ) {
  282. // Reload the page if requested
  283. if ( true === data.reload ) {
  284. window.location.reload();
  285. return;
  286. }
  287. // Remove any notices added previously
  288. $( '.woocommerce-NoticeGroup-updateOrderReview' ).remove();
  289. var termsCheckBoxChecked = $( '#terms' ).prop( 'checked' );
  290. // Save payment details to a temporary object
  291. var paymentDetails = {};
  292. $( '.payment_box :input' ).each( function() {
  293. var ID = $( this ).attr( 'id' );
  294. if ( ID ) {
  295. if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) {
  296. paymentDetails[ ID ] = $( this ).prop( 'checked' );
  297. } else {
  298. paymentDetails[ ID ] = $( this ).val();
  299. }
  300. }
  301. });
  302. // Always update the fragments
  303. if ( data && data.fragments ) {
  304. $.each( data.fragments, function ( key, value ) {
  305. $( key ).replaceWith( value );
  306. $( key ).unblock();
  307. } );
  308. }
  309. // Recheck the terms and conditions box, if needed
  310. if ( termsCheckBoxChecked ) {
  311. $( '#terms' ).prop( 'checked', true );
  312. }
  313. // Fill in the payment details if possible without overwriting data if set.
  314. if ( ! $.isEmptyObject( paymentDetails ) ) {
  315. $( '.payment_box :input' ).each( function() {
  316. var ID = $( this ).attr( 'id' );
  317. if ( ID ) {
  318. if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) {
  319. $( this ).prop( 'checked', paymentDetails[ ID ] ).change();
  320. } else if ( 0 === $( this ).val().length ) {
  321. $( this ).val( paymentDetails[ ID ] ).change();
  322. }
  323. }
  324. });
  325. }
  326. // Check for error
  327. if ( 'failure' === data.result ) {
  328. var $form = $( 'form.checkout' );
  329. // Remove notices from all sources
  330. $( '.woocommerce-error, .woocommerce-message' ).remove();
  331. // Add new errors returned by this event
  332. if ( data.messages ) {
  333. $form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-updateOrderReview">' + data.messages + '</div>' );
  334. } else {
  335. $form.prepend( data );
  336. }
  337. // Lose focus for all fields
  338. $form.find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).blur();
  339. wc_checkout_form.scroll_to_notices();
  340. }
  341. // Re-init methods
  342. wc_checkout_form.init_payment_methods();
  343. // Fire updated_checkout event.
  344. $( document.body ).trigger( 'updated_checkout', [ data ] );
  345. }
  346. });
  347. },
  348. submit: function() {
  349. wc_checkout_form.reset_update_checkout_timer();
  350. var $form = $( this );
  351. if ( $form.is( '.processing' ) ) {
  352. return false;
  353. }
  354. // Trigger a handler to let gateways manipulate the checkout if needed
  355. if ( $form.triggerHandler( 'checkout_place_order' ) !== false && $form.triggerHandler( 'checkout_place_order_' + wc_checkout_form.get_payment_method() ) !== false ) {
  356. $form.addClass( 'processing' );
  357. var form_data = $form.data();
  358. if ( 1 !== form_data['blockUI.isBlocked'] ) {
  359. $form.block({
  360. message: null,
  361. overlayCSS: {
  362. background: '#fff',
  363. opacity: 0.6
  364. }
  365. });
  366. }
  367. // ajaxSetup is global, but we use it to ensure JSON is valid once returned.
  368. $.ajaxSetup( {
  369. dataFilter: function( raw_response, dataType ) {
  370. // We only want to work with JSON
  371. if ( 'json' !== dataType ) {
  372. return raw_response;
  373. }
  374. if ( wc_checkout_form.is_valid_json( raw_response ) ) {
  375. return raw_response;
  376. } else {
  377. // Attempt to fix the malformed JSON
  378. var maybe_valid_json = raw_response.match( /{"result.*}/ );
  379. if ( null === maybe_valid_json ) {
  380. console.log( 'Unable to fix malformed JSON' );
  381. } else if ( wc_checkout_form.is_valid_json( maybe_valid_json[0] ) ) {
  382. console.log( 'Fixed malformed JSON. Original:' );
  383. console.log( raw_response );
  384. raw_response = maybe_valid_json[0];
  385. } else {
  386. console.log( 'Unable to fix malformed JSON' );
  387. }
  388. }
  389. return raw_response;
  390. }
  391. } );
  392. $.ajax({
  393. type: 'POST',
  394. url: wc_checkout_params.checkout_url,
  395. data: $form.serialize(),
  396. dataType: 'json',
  397. success: function( result ) {
  398. try {
  399. if ( 'success' === result.result ) {
  400. if ( -1 === result.redirect.indexOf( 'https://' ) || -1 === result.redirect.indexOf( 'http://' ) ) {
  401. window.location = result.redirect;
  402. } else {
  403. window.location = decodeURI( result.redirect );
  404. }
  405. } else if ( 'failure' === result.result ) {
  406. throw 'Result failure';
  407. } else {
  408. throw 'Invalid response';
  409. }
  410. } catch( err ) {
  411. // Reload page
  412. if ( true === result.reload ) {
  413. window.location.reload();
  414. return;
  415. }
  416. // Trigger update in case we need a fresh nonce
  417. if ( true === result.refresh ) {
  418. $( document.body ).trigger( 'update_checkout' );
  419. }
  420. // Add new errors
  421. if ( result.messages ) {
  422. wc_checkout_form.submit_error( result.messages );
  423. } else {
  424. wc_checkout_form.submit_error( '<div class="woocommerce-error">' + wc_checkout_params.i18n_checkout_error + '</div>' );
  425. }
  426. }
  427. },
  428. error: function( jqXHR, textStatus, errorThrown ) {
  429. wc_checkout_form.submit_error( '<div class="woocommerce-error">' + errorThrown + '</div>' );
  430. }
  431. });
  432. }
  433. return false;
  434. },
  435. submit_error: function( error_message ) {
  436. $( '.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message' ).remove();
  437. wc_checkout_form.$checkout_form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + error_message + '</div>' );
  438. wc_checkout_form.$checkout_form.removeClass( 'processing' ).unblock();
  439. wc_checkout_form.$checkout_form.find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).blur();
  440. wc_checkout_form.scroll_to_notices();
  441. $( document.body ).trigger( 'checkout_error' );
  442. },
  443. scroll_to_notices: function() {
  444. var scrollElement = $( '.woocommerce-NoticeGroup-updateOrderReview, .woocommerce-NoticeGroup-checkout' );
  445. if ( ! scrollElement.length ) {
  446. scrollElement = $( '.form.checkout' );
  447. }
  448. $.scroll_to_notices( scrollElement );
  449. }
  450. };
  451. var wc_checkout_coupons = {
  452. init: function() {
  453. $( document.body ).on( 'click', 'a.showcoupon', this.show_coupon_form );
  454. $( document.body ).on( 'click', '.woocommerce-remove-coupon', this.remove_coupon );
  455. $( 'form.checkout_coupon' ).hide().submit( this.submit );
  456. },
  457. show_coupon_form: function() {
  458. $( '.checkout_coupon' ).slideToggle( 400, function() {
  459. $( '.checkout_coupon' ).find( ':input:eq(0)' ).focus();
  460. });
  461. return false;
  462. },
  463. submit: function() {
  464. var $form = $( this );
  465. if ( $form.is( '.processing' ) ) {
  466. return false;
  467. }
  468. $form.addClass( 'processing' ).block({
  469. message: null,
  470. overlayCSS: {
  471. background: '#fff',
  472. opacity: 0.6
  473. }
  474. });
  475. var data = {
  476. security: wc_checkout_params.apply_coupon_nonce,
  477. coupon_code: $form.find( 'input[name="coupon_code"]' ).val()
  478. };
  479. $.ajax({
  480. type: 'POST',
  481. url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'apply_coupon' ),
  482. data: data,
  483. success: function( code ) {
  484. $( '.woocommerce-error, .woocommerce-message' ).remove();
  485. $form.removeClass( 'processing' ).unblock();
  486. if ( code ) {
  487. $form.before( code );
  488. $form.slideUp();
  489. $( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
  490. }
  491. },
  492. dataType: 'html'
  493. });
  494. return false;
  495. },
  496. remove_coupon: function( e ) {
  497. e.preventDefault();
  498. var container = $( this ).parents( '.woocommerce-checkout-review-order' ),
  499. coupon = $( this ).data( 'coupon' );
  500. container.addClass( 'processing' ).block({
  501. message: null,
  502. overlayCSS: {
  503. background: '#fff',
  504. opacity: 0.6
  505. }
  506. });
  507. var data = {
  508. security: wc_checkout_params.remove_coupon_nonce,
  509. coupon: coupon
  510. };
  511. $.ajax({
  512. type: 'POST',
  513. url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_coupon' ),
  514. data: data,
  515. success: function( code ) {
  516. $( '.woocommerce-error, .woocommerce-message' ).remove();
  517. container.removeClass( 'processing' ).unblock();
  518. if ( code ) {
  519. $( 'form.woocommerce-checkout' ).before( code );
  520. $( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
  521. // Remove coupon code from coupon field
  522. $( 'form.checkout_coupon' ).find( 'input[name="coupon_code"]' ).val( '' );
  523. }
  524. },
  525. error: function ( jqXHR ) {
  526. if ( wc_checkout_params.debug_mode ) {
  527. /* jshint devel: true */
  528. console.log( jqXHR.responseText );
  529. }
  530. },
  531. dataType: 'html'
  532. });
  533. }
  534. };
  535. var wc_checkout_login_form = {
  536. init: function() {
  537. $( document.body ).on( 'click', 'a.showlogin', this.show_login_form );
  538. },
  539. show_login_form: function() {
  540. $( 'form.login, form.woocommerce-form--login' ).slideToggle();
  541. return false;
  542. }
  543. };
  544. var wc_terms_toggle = {
  545. init: function() {
  546. $( document.body ).on( 'click', 'a.woocommerce-terms-and-conditions-link', this.toggle_terms );
  547. },
  548. toggle_terms: function() {
  549. if ( $( '.woocommerce-terms-and-conditions' ).length ) {
  550. $( '.woocommerce-terms-and-conditions' ).slideToggle( function() {
  551. var link_toggle = $( '.woocommerce-terms-and-conditions-link' );
  552. if ( $( '.woocommerce-terms-and-conditions' ).is( ':visible' ) ) {
  553. link_toggle.addClass( 'woocommerce-terms-and-conditions-link--open' );
  554. link_toggle.removeClass( 'woocommerce-terms-and-conditions-link--closed' );
  555. } else {
  556. link_toggle.removeClass( 'woocommerce-terms-and-conditions-link--open' );
  557. link_toggle.addClass( 'woocommerce-terms-and-conditions-link--closed' );
  558. }
  559. } );
  560. return false;
  561. }
  562. }
  563. };
  564. wc_checkout_form.init();
  565. wc_checkout_coupons.init();
  566. wc_checkout_login_form.init();
  567. wc_terms_toggle.init();
  568. });