PageRenderTime 63ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.js

https://gitlab.com/campus-academy/krowkaramel
JavaScript | 214 lines | 144 code | 37 blank | 33 comment | 19 complexity | 630ce19231de0d10f7998650bb376955 MD5 | raw file
  1. /* global wc_add_to_cart_params */
  2. jQuery( function( $ ) {
  3. if ( typeof wc_add_to_cart_params === 'undefined' ) {
  4. return false;
  5. }
  6. /**
  7. * AddToCartHandler class.
  8. */
  9. var AddToCartHandler = function() {
  10. this.requests = [];
  11. this.addRequest = this.addRequest.bind( this );
  12. this.run = this.run.bind( this );
  13. $( document.body )
  14. .on( 'click', '.add_to_cart_button', { addToCartHandler: this }, this.onAddToCart )
  15. .on( 'click', '.remove_from_cart_button', { addToCartHandler: this }, this.onRemoveFromCart )
  16. .on( 'added_to_cart', this.updateButton )
  17. .on( 'ajax_request_not_sent.adding_to_cart', this.updateButton )
  18. .on( 'added_to_cart removed_from_cart', { addToCartHandler: this }, this.updateFragments );
  19. };
  20. /**
  21. * Add add to cart event.
  22. */
  23. AddToCartHandler.prototype.addRequest = function( request ) {
  24. this.requests.push( request );
  25. if ( 1 === this.requests.length ) {
  26. this.run();
  27. }
  28. };
  29. /**
  30. * Run add to cart events.
  31. */
  32. AddToCartHandler.prototype.run = function() {
  33. var requestManager = this,
  34. originalCallback = requestManager.requests[0].complete;
  35. requestManager.requests[0].complete = function() {
  36. if ( typeof originalCallback === 'function' ) {
  37. originalCallback();
  38. }
  39. requestManager.requests.shift();
  40. if ( requestManager.requests.length > 0 ) {
  41. requestManager.run();
  42. }
  43. };
  44. $.ajax( this.requests[0] );
  45. };
  46. /**
  47. * Handle the add to cart event.
  48. */
  49. AddToCartHandler.prototype.onAddToCart = function( e ) {
  50. var $thisbutton = $( this );
  51. if ( $thisbutton.is( '.ajax_add_to_cart' ) ) {
  52. if ( ! $thisbutton.attr( 'data-product_id' ) ) {
  53. return true;
  54. }
  55. e.preventDefault();
  56. $thisbutton.removeClass( 'added' );
  57. $thisbutton.addClass( 'loading' );
  58. // Allow 3rd parties to validate and quit early.
  59. if ( false === $( document.body ).triggerHandler( 'should_send_ajax_request.adding_to_cart', [ $thisbutton ] ) ) {
  60. $( document.body ).trigger( 'ajax_request_not_sent.adding_to_cart', [ false, false, $thisbutton ] );
  61. return true;
  62. }
  63. var data = {};
  64. // Fetch changes that are directly added by calling $thisbutton.data( key, value )
  65. $.each( $thisbutton.data(), function( key, value ) {
  66. data[ key ] = value;
  67. });
  68. // Fetch data attributes in $thisbutton. Give preference to data-attributes because they can be directly modified by javascript
  69. // while `.data` are jquery specific memory stores.
  70. $.each( $thisbutton[0].dataset, function( key, value ) {
  71. data[ key ] = value;
  72. });
  73. // Trigger event.
  74. $( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
  75. e.data.addToCartHandler.addRequest({
  76. type: 'POST',
  77. url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ),
  78. data: data,
  79. success: function( response ) {
  80. if ( ! response ) {
  81. return;
  82. }
  83. if ( response.error && response.product_url ) {
  84. window.location = response.product_url;
  85. return;
  86. }
  87. // Redirect to cart option
  88. if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
  89. window.location = wc_add_to_cart_params.cart_url;
  90. return;
  91. }
  92. // Trigger event so themes can refresh other areas.
  93. $( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
  94. },
  95. dataType: 'json'
  96. });
  97. }
  98. };
  99. /**
  100. * Update fragments after remove from cart event in mini-cart.
  101. */
  102. AddToCartHandler.prototype.onRemoveFromCart = function( e ) {
  103. var $thisbutton = $( this ),
  104. $row = $thisbutton.closest( '.woocommerce-mini-cart-item' );
  105. e.preventDefault();
  106. $row.block({
  107. message: null,
  108. overlayCSS: {
  109. opacity: 0.6
  110. }
  111. });
  112. e.data.addToCartHandler.addRequest({
  113. type: 'POST',
  114. url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_from_cart' ),
  115. data: {
  116. cart_item_key : $thisbutton.data( 'cart_item_key' )
  117. },
  118. success: function( response ) {
  119. if ( ! response || ! response.fragments ) {
  120. window.location = $thisbutton.attr( 'href' );
  121. return;
  122. }
  123. $( document.body ).trigger( 'removed_from_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
  124. },
  125. error: function() {
  126. window.location = $thisbutton.attr( 'href' );
  127. return;
  128. },
  129. dataType: 'json'
  130. });
  131. };
  132. /**
  133. * Update cart page elements after add to cart events.
  134. */
  135. AddToCartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) {
  136. $button = typeof $button === 'undefined' ? false : $button;
  137. if ( $button ) {
  138. $button.removeClass( 'loading' );
  139. if ( fragments ) {
  140. $button.addClass( 'added' );
  141. }
  142. // View cart text.
  143. if ( fragments && ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) {
  144. $button.after( '<a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' +
  145. wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' );
  146. }
  147. $( document.body ).trigger( 'wc_cart_button_updated', [ $button ] );
  148. }
  149. };
  150. /**
  151. * Update fragments after add to cart events.
  152. */
  153. AddToCartHandler.prototype.updateFragments = function( e, fragments ) {
  154. if ( fragments ) {
  155. $.each( fragments, function( key ) {
  156. $( key )
  157. .addClass( 'updating' )
  158. .fadeTo( '400', '0.6' )
  159. .block({
  160. message: null,
  161. overlayCSS: {
  162. opacity: 0.6
  163. }
  164. });
  165. });
  166. $.each( fragments, function( key, value ) {
  167. $( key ).replaceWith( value );
  168. $( key ).stop( true ).css( 'opacity', '1' ).unblock();
  169. });
  170. $( document.body ).trigger( 'wc_fragments_loaded' );
  171. }
  172. };
  173. /**
  174. * Init AddToCartHandler.
  175. */
  176. new AddToCartHandler();
  177. });