cart-fragments.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. success: function( data ) {
  36. if ( data && data.fragments ) {
  37. $.each( data.fragments, function( key, value ) {
  38. $( key ).replaceWith( value );
  39. });
  40. if ( $supports_html5_storage ) {
  41. sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) );
  42. set_cart_hash( data.cart_hash );
  43. if ( data.cart_hash ) {
  44. set_cart_creation_timestamp();
  45. }
  46. }
  47. $( document.body ).trigger( 'wc_fragments_refreshed' );
  48. }
  49. }
  50. };
  51. /* Named callback for refreshing cart fragment */
  52. function refresh_cart_fragment() {
  53. $.ajax( $fragment_refresh );
  54. }
  55. /* Cart Handling */
  56. if ( $supports_html5_storage ) {
  57. var cart_timeout = null,
  58. day_in_ms = ( 24 * 60 * 60 * 1000 );
  59. $( document.body ).on( 'wc_fragment_refresh updated_wc_div', function() {
  60. refresh_cart_fragment();
  61. });
  62. $( document.body ).on( 'added_to_cart removed_from_cart', function( event, fragments, cart_hash ) {
  63. var prev_cart_hash = sessionStorage.getItem( cart_hash_key );
  64. if ( prev_cart_hash === null || prev_cart_hash === undefined || prev_cart_hash === '' ) {
  65. set_cart_creation_timestamp();
  66. }
  67. sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( fragments ) );
  68. set_cart_hash( cart_hash );
  69. });
  70. $( document.body ).on( 'wc_fragments_refreshed', function() {
  71. clearTimeout( cart_timeout );
  72. cart_timeout = setTimeout( refresh_cart_fragment, day_in_ms );
  73. } );
  74. // Refresh when storage changes in another tab
  75. $( window ).on( 'storage onstorage', function ( e ) {
  76. if ( cart_hash_key === e.originalEvent.key && localStorage.getItem( cart_hash_key ) !== sessionStorage.getItem( cart_hash_key ) ) {
  77. refresh_cart_fragment();
  78. }
  79. });
  80. // Refresh when page is shown after back button (safari)
  81. $( window ).on( 'pageshow' , function( e ) {
  82. if ( e.originalEvent.persisted ) {
  83. $( '.widget_shopping_cart_content' ).empty();
  84. $( document.body ).trigger( 'wc_fragment_refresh' );
  85. }
  86. } );
  87. try {
  88. var wc_fragments = $.parseJSON( sessionStorage.getItem( wc_cart_fragments_params.fragment_name ) ),
  89. cart_hash = sessionStorage.getItem( cart_hash_key ),
  90. cookie_hash = Cookies.get( 'woocommerce_cart_hash'),
  91. cart_created = sessionStorage.getItem( 'wc_cart_created' );
  92. if ( cart_hash === null || cart_hash === undefined || cart_hash === '' ) {
  93. cart_hash = '';
  94. }
  95. if ( cookie_hash === null || cookie_hash === undefined || cookie_hash === '' ) {
  96. cookie_hash = '';
  97. }
  98. if ( cart_hash && ( cart_created === null || cart_created === undefined || cart_created === '' ) ) {
  99. throw 'No cart_created';
  100. }
  101. if ( cart_created ) {
  102. var cart_expiration = ( ( 1 * cart_created ) + day_in_ms ),
  103. timestamp_now = ( new Date() ).getTime();
  104. if ( cart_expiration < timestamp_now ) {
  105. throw 'Fragment expired';
  106. }
  107. cart_timeout = setTimeout( refresh_cart_fragment, ( cart_expiration - timestamp_now ) );
  108. }
  109. if ( wc_fragments && wc_fragments['div.widget_shopping_cart_content'] && cart_hash === cookie_hash ) {
  110. $.each( wc_fragments, function( key, value ) {
  111. $( key ).replaceWith(value);
  112. });
  113. $( document.body ).trigger( 'wc_fragments_loaded' );
  114. } else {
  115. throw 'No fragment';
  116. }
  117. } catch( err ) {
  118. refresh_cart_fragment();
  119. }
  120. } else {
  121. refresh_cart_fragment();
  122. }
  123. /* Cart Hiding */
  124. if ( Cookies.get( 'woocommerce_items_in_cart' ) > 0 ) {
  125. $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
  126. } else {
  127. $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).hide();
  128. }
  129. $( document.body ).on( 'adding_to_cart', function() {
  130. $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
  131. });
  132. });