add-to-cart.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. $( document.body )
  11. .on( 'click', '.add_to_cart_button', this.onAddToCart )
  12. .on( 'click', '.remove_from_cart_button', this.onRemoveFromCart )
  13. .on( 'added_to_cart', this.updateButton )
  14. .on( 'added_to_cart', this.updateCartPage )
  15. .on( 'added_to_cart removed_from_cart', this.updateFragments );
  16. };
  17. /**
  18. * Handle the add to cart event.
  19. */
  20. AddToCartHandler.prototype.onAddToCart = function( e ) {
  21. var $thisbutton = $( this );
  22. if ( $thisbutton.is( '.ajax_add_to_cart' ) ) {
  23. if ( ! $thisbutton.attr( 'data-product_id' ) ) {
  24. return true;
  25. }
  26. e.preventDefault();
  27. $thisbutton.removeClass( 'added' );
  28. $thisbutton.addClass( 'loading' );
  29. var data = {};
  30. $.each( $thisbutton.data(), function( key, value ) {
  31. data[ key ] = value;
  32. });
  33. // Trigger event.
  34. $( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
  35. // Ajax action.
  36. $.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), data, function( response ) {
  37. if ( ! response ) {
  38. return;
  39. }
  40. if ( response.error && response.product_url ) {
  41. window.location = response.product_url;
  42. return;
  43. }
  44. // Redirect to cart option
  45. if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
  46. window.location = wc_add_to_cart_params.cart_url;
  47. return;
  48. }
  49. // Trigger event so themes can refresh other areas.
  50. $( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
  51. });
  52. }
  53. };
  54. /**
  55. * Update fragments after remove from cart event in mini-cart.
  56. */
  57. AddToCartHandler.prototype.onRemoveFromCart = function( e ) {
  58. var $thisbutton = $( this ),
  59. $row = $thisbutton.closest( '.woocommerce-mini-cart-item' );
  60. e.preventDefault();
  61. $row.block({
  62. message: null,
  63. overlayCSS: {
  64. opacity: 0.6
  65. }
  66. });
  67. $.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_from_cart' ), { cart_item_key : $thisbutton.data( 'cart_item_key' ) }, function( response ) {
  68. if ( ! response || ! response.fragments ) {
  69. window.location = $thisbutton.attr( 'href' );
  70. return;
  71. }
  72. $( document.body ).trigger( 'removed_from_cart', [ response.fragments, response.cart_hash ] );
  73. }).fail( function() {
  74. window.location = $thisbutton.attr( 'href' );
  75. return;
  76. });
  77. };
  78. /**
  79. * Update cart page elements after add to cart events.
  80. */
  81. AddToCartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) {
  82. $button = typeof $button === 'undefined' ? false : $button;
  83. if ( $button ) {
  84. $button.removeClass( 'loading' );
  85. $button.addClass( 'added' );
  86. // View cart text.
  87. if ( ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) {
  88. $button.after( ' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' +
  89. wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' );
  90. }
  91. $( document.body ).trigger( 'wc_cart_button_updated', [ $button ] );
  92. }
  93. };
  94. /**
  95. * Update cart page elements after add to cart events.
  96. */
  97. AddToCartHandler.prototype.updateCartPage = function() {
  98. var page = window.location.toString().replace( 'add-to-cart', 'added-to-cart' );
  99. $( '.shop_table.cart' ).load( page + ' .shop_table.cart:eq(0) > *', function() {
  100. $( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
  101. $( document.body ).trigger( 'cart_page_refreshed' );
  102. });
  103. $( '.cart_totals' ).load( page + ' .cart_totals:eq(0) > *', function() {
  104. $( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock();
  105. $( document.body ).trigger( 'cart_totals_refreshed' );
  106. });
  107. };
  108. /**
  109. * Update fragments after add to cart events.
  110. */
  111. AddToCartHandler.prototype.updateFragments = function( e, fragments ) {
  112. if ( fragments ) {
  113. $.each( fragments, function( key ) {
  114. $( key )
  115. .addClass( 'updating' )
  116. .fadeTo( '400', '0.6' )
  117. .block({
  118. message: null,
  119. overlayCSS: {
  120. opacity: 0.6
  121. }
  122. });
  123. });
  124. $.each( fragments, function( key, value ) {
  125. $( key ).replaceWith( value );
  126. $( key ).stop( true ).css( 'opacity', '1' ).unblock();
  127. });
  128. $( document.body ).trigger( 'wc_fragments_loaded' );
  129. }
  130. };
  131. /**
  132. * Init AddToCartHandler.
  133. */
  134. new AddToCartHandler();
  135. });