Nav apraksta

cart-fragments.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* global wc_cart_fragments_params, Cookies */
  2. jQuery( function( $ ) {
  3. // wc_cart_fragments_params is required to continue, ensure the object exists
  4. if ( typeof wc_cart_fragments_params === 'undefined' ) {
  5. return false;
  6. }
  7. /* Storage Handling */
  8. var $supports_html5_storage = true,
  9. cart_hash_key = wc_cart_fragments_params.cart_hash_key;
  10. try {
  11. $supports_html5_storage = ( 'sessionStorage' in window && window.sessionStorage !== null );
  12. window.sessionStorage.setItem( 'wc', 'test' );
  13. window.sessionStorage.removeItem( 'wc' );
  14. window.localStorage.setItem( 'wc', 'test' );
  15. window.localStorage.removeItem( 'wc' );
  16. } catch( err ) {
  17. $supports_html5_storage = false;
  18. }
  19. /* Cart session creation time to base expiration on */
  20. function set_cart_creation_timestamp() {
  21. if ( $supports_html5_storage ) {
  22. sessionStorage.setItem( 'wc_cart_created', ( new Date() ).getTime() );
  23. }
  24. }
  25. /** Set the cart hash in both session and local storage */
  26. function set_cart_hash( cart_hash ) {
  27. if ( $supports_html5_storage ) {
  28. localStorage.setItem( cart_hash_key, cart_hash );
  29. sessionStorage.setItem( cart_hash_key, cart_hash );
  30. }
  31. }
  32. var $fragment_refresh = {
  33. url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_refreshed_fragments' ),
  34. type: 'POST',
  35. data: {
  36. time: new Date().getTime()
  37. },
  38. timeout: wc_cart_fragments_params.request_timeout,
  39. success: function( data ) {
  40. if ( data && data.fragments ) {
  41. $.each( data.fragments, function( key, value ) {
  42. $( key ).replaceWith( value );
  43. });
  44. if ( $supports_html5_storage ) {
  45. sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) );
  46. set_cart_hash( data.cart_hash );
  47. if ( data.cart_hash ) {
  48. set_cart_creation_timestamp();
  49. }
  50. }
  51. $( document.body ).trigger( 'wc_fragments_refreshed' );
  52. }
  53. },
  54. error: function() {
  55. $( document.body ).trigger( 'wc_fragments_ajax_error' );
  56. }
  57. };
  58. /* Named callback for refreshing cart fragment */
  59. function refresh_cart_fragment() {
  60. $.ajax( $fragment_refresh );
  61. }
  62. /* Cart Handling */
  63. if ( $supports_html5_storage ) {
  64. var cart_timeout = null,
  65. day_in_ms = ( 24 * 60 * 60 * 1000 );
  66. $( document.body ).on( 'wc_fragment_refresh updated_wc_div', function() {
  67. refresh_cart_fragment();
  68. });
  69. $( document.body ).on( 'added_to_cart removed_from_cart', function( event, fragments, cart_hash ) {
  70. var prev_cart_hash = sessionStorage.getItem( cart_hash_key );
  71. if ( prev_cart_hash === null || prev_cart_hash === undefined || prev_cart_hash === '' ) {
  72. set_cart_creation_timestamp();
  73. }
  74. sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( fragments ) );
  75. set_cart_hash( cart_hash );
  76. });
  77. $( document.body ).on( 'wc_fragments_refreshed', function() {
  78. clearTimeout( cart_timeout );
  79. cart_timeout = setTimeout( refresh_cart_fragment, day_in_ms );
  80. } );
  81. // Refresh when storage changes in another tab
  82. $( window ).on( 'storage onstorage', function ( e ) {
  83. if (
  84. cart_hash_key === e.originalEvent.key && localStorage.getItem( cart_hash_key ) !== sessionStorage.getItem( cart_hash_key )
  85. ) {
  86. refresh_cart_fragment();
  87. }
  88. });
  89. // Refresh when page is shown after back button (safari)
  90. $( window ).on( 'pageshow' , function( e ) {
  91. if ( e.originalEvent.persisted ) {
  92. $( '.widget_shopping_cart_content' ).empty();
  93. $( document.body ).trigger( 'wc_fragment_refresh' );
  94. }
  95. } );
  96. try {
  97. var wc_fragments = JSON.parse( sessionStorage.getItem( wc_cart_fragments_params.fragment_name ) ),
  98. cart_hash = sessionStorage.getItem( cart_hash_key ),
  99. cookie_hash = Cookies.get( 'woocommerce_cart_hash'),
  100. cart_created = sessionStorage.getItem( 'wc_cart_created' );
  101. if ( cart_hash === null || cart_hash === undefined || cart_hash === '' ) {
  102. cart_hash = '';
  103. }
  104. if ( cookie_hash === null || cookie_hash === undefined || cookie_hash === '' ) {
  105. cookie_hash = '';
  106. }
  107. if ( cart_hash && ( cart_created === null || cart_created === undefined || cart_created === '' ) ) {
  108. throw 'No cart_created';
  109. }
  110. if ( cart_created ) {
  111. var cart_expiration = ( ( 1 * cart_created ) + day_in_ms ),
  112. timestamp_now = ( new Date() ).getTime();
  113. if ( cart_expiration < timestamp_now ) {
  114. throw 'Fragment expired';
  115. }
  116. cart_timeout = setTimeout( refresh_cart_fragment, ( cart_expiration - timestamp_now ) );
  117. }
  118. if ( wc_fragments && wc_fragments['div.widget_shopping_cart_content'] && cart_hash === cookie_hash ) {
  119. $.each( wc_fragments, function( key, value ) {
  120. $( key ).replaceWith(value);
  121. });
  122. $( document.body ).trigger( 'wc_fragments_loaded' );
  123. } else {
  124. throw 'No fragment';
  125. }
  126. } catch( err ) {
  127. refresh_cart_fragment();
  128. }
  129. } else {
  130. refresh_cart_fragment();
  131. }
  132. /* Cart Hiding */
  133. if ( Cookies.get( 'woocommerce_items_in_cart' ) > 0 ) {
  134. $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
  135. } else {
  136. $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).hide();
  137. }
  138. $( document.body ).on( 'adding_to_cart', function() {
  139. $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
  140. });
  141. // Customiser support.
  142. var hasSelectiveRefresh = (
  143. 'undefined' !== typeof wp &&
  144. wp.customize &&
  145. wp.customize.selectiveRefresh &&
  146. wp.customize.widgetsPreview &&
  147. wp.customize.widgetsPreview.WidgetPartial
  148. );
  149. if ( hasSelectiveRefresh ) {
  150. wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function() {
  151. refresh_cart_fragment();
  152. } );
  153. }
  154. });