1 /* global wc_cart_fragments_params, Cookies */
2 jQuery( function( $ ) {
4 // wc_cart_fragments_params is required to continue, ensure the object exists
5 if ( typeof wc_cart_fragments_params === 'undefined' ) {
10 var $supports_html5_storage;
11 var cart_hash_key = wc_cart_fragments_params.ajax_url.toString() + '-wc_cart_hash';
14 $supports_html5_storage = ( 'sessionStorage' in window && window.sessionStorage !== null );
15 window.sessionStorage.setItem( 'wc', 'test' );
16 window.sessionStorage.removeItem( 'wc' );
17 window.localStorage.setItem( 'wc', 'test' );
18 window.localStorage.removeItem( 'wc' );
20 $supports_html5_storage = false;
23 /* Cart session creation time to base expiration on */
24 function set_cart_creation_timestamp() {
25 if ( $supports_html5_storage ) {
26 sessionStorage.setItem( 'wc_cart_created', ( new Date() ).getTime() );
30 /** Set the cart hash in both session and local storage */
31 function set_cart_hash( cart_hash ) {
32 if ( $supports_html5_storage ) {
33 localStorage.setItem( cart_hash_key, cart_hash );
34 sessionStorage.setItem( cart_hash_key, cart_hash );
38 var $fragment_refresh = {
39 url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_refreshed_fragments' ),
41 success: function( data ) {
42 if ( data && data.fragments ) {
44 $.each( data.fragments, function( key, value ) {
45 $( key ).replaceWith( value );
48 if ( $supports_html5_storage ) {
49 sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) );
50 set_cart_hash( data.cart_hash );
52 if ( data.cart_hash ) {
53 set_cart_creation_timestamp();
57 $( document.body ).trigger( 'wc_fragments_refreshed' );
62 /* Named callback for refreshing cart fragment */
63 function refresh_cart_fragment() {
64 $.ajax( $fragment_refresh );
68 if ( $supports_html5_storage ) {
70 var cart_timeout = null,
71 day_in_ms = ( 24 * 60 * 60 * 1000 );
73 $( document.body ).on( 'wc_fragment_refresh updated_wc_div', function() {
74 refresh_cart_fragment();
77 $( document.body ).on( 'added_to_cart', function( event, fragments, cart_hash ) {
78 var prev_cart_hash = sessionStorage.getItem( cart_hash_key );
80 if ( prev_cart_hash === null || prev_cart_hash === undefined || prev_cart_hash === '' ) {
81 set_cart_creation_timestamp();
84 sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( fragments ) );
85 set_cart_hash( cart_hash );
88 $( document.body ).on( 'wc_fragments_refreshed', function() {
89 clearTimeout( cart_timeout );
90 cart_timeout = setTimeout( refresh_cart_fragment, day_in_ms );
93 // Refresh when storage changes in another tab
94 $( window ).on( 'storage onstorage', function ( e ) {
95 if ( cart_hash_key === e.originalEvent.key && localStorage.getItem( cart_hash_key ) !== sessionStorage.getItem( cart_hash_key ) ) {
96 refresh_cart_fragment();
100 // Refresh when page is shown after back button (safari)
101 $( window ).on( 'pageshow' , function( e ) {
102 if ( e.originalEvent.persisted ) {
103 $( '.widget_shopping_cart_content' ).empty();
104 $( document.body ).trigger( 'wc_fragment_refresh' );
109 var wc_fragments = $.parseJSON( sessionStorage.getItem( wc_cart_fragments_params.fragment_name ) ),
110 cart_hash = sessionStorage.getItem( cart_hash_key ),
111 cookie_hash = Cookies.get( 'woocommerce_cart_hash'),
112 cart_created = sessionStorage.getItem( 'wc_cart_created' );
114 if ( cart_hash === null || cart_hash === undefined || cart_hash === '' ) {
118 if ( cookie_hash === null || cookie_hash === undefined || cookie_hash === '' ) {
122 if ( cart_hash && ( cart_created === null || cart_created === undefined || cart_created === '' ) ) {
123 throw 'No cart_created';
126 if ( cart_created ) {
127 var cart_expiration = ( ( 1 * cart_created ) + day_in_ms ),
128 timestamp_now = ( new Date() ).getTime();
129 if ( cart_expiration < timestamp_now ) {
130 throw 'Fragment expired';
132 cart_timeout = setTimeout( refresh_cart_fragment, ( cart_expiration - timestamp_now ) );
135 if ( wc_fragments && wc_fragments['div.widget_shopping_cart_content'] && cart_hash === cookie_hash ) {
137 $.each( wc_fragments, function( key, value ) {
138 $( key ).replaceWith(value);
141 $( document.body ).trigger( 'wc_fragments_loaded' );
147 refresh_cart_fragment();
151 refresh_cart_fragment();
155 if ( Cookies.get( 'woocommerce_items_in_cart' ) > 0 ) {
156 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
158 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).hide();
161 $( document.body ).on( 'adding_to_cart', function() {
162 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();