2016-11-22 15:09:37 +00:00
|
|
|
/* global wc_cart_fragments_params, Cookies */
|
2014-03-18 02:56:31 +00:00
|
|
|
jQuery( function( $ ) {
|
2013-02-26 12:15:12 +00:00
|
|
|
|
2013-12-04 19:15:24 +00:00
|
|
|
// wc_cart_fragments_params is required to continue, ensure the object exists
|
2014-03-18 02:56:31 +00:00
|
|
|
if ( typeof wc_cart_fragments_params === 'undefined' ) {
|
2013-12-04 19:15:24 +00:00
|
|
|
return false;
|
2014-03-18 02:56:31 +00:00
|
|
|
}
|
2013-12-04 19:15:24 +00:00
|
|
|
|
2015-06-10 17:26:21 +00:00
|
|
|
/* Storage Handling */
|
2018-01-15 16:03:27 +00:00
|
|
|
var $supports_html5_storage = true,
|
|
|
|
cart_hash_key = wc_cart_fragments_params.cart_hash_key;
|
2016-03-13 15:42:35 +00:00
|
|
|
|
2014-04-07 13:34:47 +00:00
|
|
|
try {
|
|
|
|
$supports_html5_storage = ( 'sessionStorage' in window && window.sessionStorage !== null );
|
2014-11-14 19:11:23 +00:00
|
|
|
window.sessionStorage.setItem( 'wc', 'test' );
|
|
|
|
window.sessionStorage.removeItem( 'wc' );
|
2016-03-13 15:42:35 +00:00
|
|
|
window.localStorage.setItem( 'wc', 'test' );
|
|
|
|
window.localStorage.removeItem( 'wc' );
|
2014-04-07 13:34:47 +00:00
|
|
|
} catch( err ) {
|
|
|
|
$supports_html5_storage = false;
|
|
|
|
}
|
2013-02-26 12:15:12 +00:00
|
|
|
|
2015-10-06 15:54:06 +00:00
|
|
|
/* Cart session creation time to base expiration on */
|
|
|
|
function set_cart_creation_timestamp() {
|
|
|
|
if ( $supports_html5_storage ) {
|
|
|
|
sessionStorage.setItem( 'wc_cart_created', ( new Date() ).getTime() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 11:50:25 +00:00
|
|
|
/** Set the cart hash in both session and local storage */
|
|
|
|
function set_cart_hash( cart_hash ) {
|
|
|
|
if ( $supports_html5_storage ) {
|
2016-05-24 19:36:43 +00:00
|
|
|
localStorage.setItem( cart_hash_key, cart_hash );
|
|
|
|
sessionStorage.setItem( cart_hash_key, cart_hash );
|
2015-10-26 11:50:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:26:21 +00:00
|
|
|
var $fragment_refresh = {
|
2015-07-27 16:55:37 +00:00
|
|
|
url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_refreshed_fragments' ),
|
2013-02-26 12:15:12 +00:00
|
|
|
type: 'POST',
|
2019-02-15 17:35:44 +00:00
|
|
|
data: {
|
|
|
|
time: new Date().getTime()
|
|
|
|
},
|
2019-01-23 15:16:40 +00:00
|
|
|
timeout: wc_cart_fragments_params.request_timeout,
|
2013-02-26 12:15:12 +00:00
|
|
|
success: function( data ) {
|
|
|
|
if ( data && data.fragments ) {
|
|
|
|
|
|
|
|
$.each( data.fragments, function( key, value ) {
|
2014-03-18 02:56:31 +00:00
|
|
|
$( key ).replaceWith( value );
|
2013-02-26 12:15:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if ( $supports_html5_storage ) {
|
2013-11-20 16:31:28 +00:00
|
|
|
sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) );
|
2015-10-26 11:50:25 +00:00
|
|
|
set_cart_hash( data.cart_hash );
|
2015-10-06 15:54:06 +00:00
|
|
|
|
|
|
|
if ( data.cart_hash ) {
|
|
|
|
set_cart_creation_timestamp();
|
|
|
|
}
|
2013-02-26 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).trigger( 'wc_fragments_refreshed' );
|
2013-02-26 12:15:12 +00:00
|
|
|
}
|
2018-08-14 10:25:29 +00:00
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
$( document.body ).trigger( 'wc_fragments_ajax_error' );
|
2013-02-26 12:15:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-06 18:14:17 +00:00
|
|
|
/* Named callback for refreshing cart fragment */
|
|
|
|
function refresh_cart_fragment() {
|
|
|
|
$.ajax( $fragment_refresh );
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:26:21 +00:00
|
|
|
/* Cart Handling */
|
2013-02-26 12:15:12 +00:00
|
|
|
if ( $supports_html5_storage ) {
|
|
|
|
|
2015-10-06 18:14:17 +00:00
|
|
|
var cart_timeout = null,
|
|
|
|
day_in_ms = ( 24 * 60 * 60 * 1000 );
|
|
|
|
|
2017-02-15 09:43:16 +00:00
|
|
|
$( document.body ).on( 'wc_fragment_refresh updated_wc_div', function() {
|
2016-06-08 11:06:40 +00:00
|
|
|
refresh_cart_fragment();
|
|
|
|
});
|
|
|
|
|
2018-01-22 10:50:57 +00:00
|
|
|
$( document.body ).on( 'added_to_cart removed_from_cart', function( event, fragments, cart_hash ) {
|
2016-05-24 19:36:43 +00:00
|
|
|
var prev_cart_hash = sessionStorage.getItem( cart_hash_key );
|
2015-10-06 15:54:06 +00:00
|
|
|
|
|
|
|
if ( prev_cart_hash === null || prev_cart_hash === undefined || prev_cart_hash === '' ) {
|
|
|
|
set_cart_creation_timestamp();
|
|
|
|
}
|
|
|
|
|
2013-11-20 16:31:28 +00:00
|
|
|
sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( fragments ) );
|
2015-10-26 11:50:25 +00:00
|
|
|
set_cart_hash( cart_hash );
|
2013-02-26 12:15:12 +00:00
|
|
|
});
|
|
|
|
|
2017-02-15 09:43:16 +00:00
|
|
|
$( document.body ).on( 'wc_fragments_refreshed', function() {
|
2015-10-06 18:14:17 +00:00
|
|
|
clearTimeout( cart_timeout );
|
|
|
|
cart_timeout = setTimeout( refresh_cart_fragment, day_in_ms );
|
|
|
|
} );
|
|
|
|
|
2015-10-26 11:50:25 +00:00
|
|
|
// Refresh when storage changes in another tab
|
|
|
|
$( window ).on( 'storage onstorage', function ( e ) {
|
2019-02-15 17:10:17 +00:00
|
|
|
if (
|
2019-03-15 12:21:51 +00:00
|
|
|
cart_hash_key === e.originalEvent.key && localStorage.getItem( cart_hash_key ) !== sessionStorage.getItem( cart_hash_key )
|
2019-02-15 17:10:17 +00:00
|
|
|
) {
|
2016-05-24 19:36:43 +00:00
|
|
|
refresh_cart_fragment();
|
2015-10-26 11:50:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-15 17:35:44 +00:00
|
|
|
// Refresh when page is shown after back button (safari)
|
|
|
|
$( window ).on( 'pageshow' , function( e ) {
|
|
|
|
if ( e.originalEvent.persisted ) {
|
|
|
|
$( '.widget_shopping_cart_content' ).empty();
|
|
|
|
$( document.body ).trigger( 'wc_fragment_refresh' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2013-02-26 12:15:12 +00:00
|
|
|
try {
|
2020-10-22 13:11:27 +00:00
|
|
|
var wc_fragments = JSON.parse( sessionStorage.getItem( wc_cart_fragments_params.fragment_name ) ),
|
2016-05-24 19:36:43 +00:00
|
|
|
cart_hash = sessionStorage.getItem( cart_hash_key ),
|
2016-11-22 15:09:37 +00:00
|
|
|
cookie_hash = Cookies.get( 'woocommerce_cart_hash'),
|
2015-10-06 18:14:17 +00:00
|
|
|
cart_created = sessionStorage.getItem( 'wc_cart_created' );
|
2013-02-26 12:15:12 +00:00
|
|
|
|
2014-03-18 02:56:31 +00:00
|
|
|
if ( cart_hash === null || cart_hash === undefined || cart_hash === '' ) {
|
2013-07-30 14:23:08 +00:00
|
|
|
cart_hash = '';
|
2014-03-18 02:56:31 +00:00
|
|
|
}
|
2013-07-30 14:23:08 +00:00
|
|
|
|
2014-03-18 02:56:31 +00:00
|
|
|
if ( cookie_hash === null || cookie_hash === undefined || cookie_hash === '' ) {
|
2013-07-30 14:23:08 +00:00
|
|
|
cookie_hash = '';
|
2014-03-18 02:56:31 +00:00
|
|
|
}
|
2013-07-30 14:23:08 +00:00
|
|
|
|
2015-10-06 16:04:21 +00:00
|
|
|
if ( cart_hash && ( cart_created === null || cart_created === undefined || cart_created === '' ) ) {
|
|
|
|
throw 'No cart_created';
|
|
|
|
}
|
|
|
|
|
2015-10-06 18:14:17 +00:00
|
|
|
if ( cart_created ) {
|
|
|
|
var cart_expiration = ( ( 1 * cart_created ) + day_in_ms ),
|
|
|
|
timestamp_now = ( new Date() ).getTime();
|
|
|
|
if ( cart_expiration < timestamp_now ) {
|
|
|
|
throw 'Fragment expired';
|
|
|
|
}
|
|
|
|
cart_timeout = setTimeout( refresh_cart_fragment, ( cart_expiration - timestamp_now ) );
|
2015-10-06 17:12:18 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 17:26:21 +00:00
|
|
|
if ( wc_fragments && wc_fragments['div.widget_shopping_cart_content'] && cart_hash === cookie_hash ) {
|
2013-02-26 12:15:12 +00:00
|
|
|
|
|
|
|
$.each( wc_fragments, function( key, value ) {
|
2014-03-18 02:56:31 +00:00
|
|
|
$( key ).replaceWith(value);
|
2013-02-26 12:15:12 +00:00
|
|
|
});
|
|
|
|
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).trigger( 'wc_fragments_loaded' );
|
2013-02-26 12:15:12 +00:00
|
|
|
} else {
|
2014-03-18 02:56:31 +00:00
|
|
|
throw 'No fragment';
|
2013-02-26 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 02:56:31 +00:00
|
|
|
} catch( err ) {
|
2015-10-06 18:16:15 +00:00
|
|
|
refresh_cart_fragment();
|
2013-02-26 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2015-10-06 18:16:15 +00:00
|
|
|
refresh_cart_fragment();
|
2013-02-26 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 17:26:21 +00:00
|
|
|
/* Cart Hiding */
|
2016-11-22 15:09:37 +00:00
|
|
|
if ( Cookies.get( 'woocommerce_items_in_cart' ) > 0 ) {
|
2014-03-18 02:56:31 +00:00
|
|
|
$( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
|
|
|
|
} else {
|
|
|
|
$( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).hide();
|
|
|
|
}
|
2013-04-09 08:38:55 +00:00
|
|
|
|
2017-02-15 09:43:16 +00:00
|
|
|
$( document.body ).on( 'adding_to_cart', function() {
|
2014-03-18 02:56:31 +00:00
|
|
|
$( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
|
2015-06-10 17:26:21 +00:00
|
|
|
});
|
2019-01-23 21:29:55 +00:00
|
|
|
|
|
|
|
// Customiser support.
|
|
|
|
var hasSelectiveRefresh = (
|
|
|
|
'undefined' !== typeof wp &&
|
|
|
|
wp.customize &&
|
|
|
|
wp.customize.selectiveRefresh &&
|
|
|
|
wp.customize.widgetsPreview &&
|
|
|
|
wp.customize.widgetsPreview.WidgetPartial
|
|
|
|
);
|
|
|
|
if ( hasSelectiveRefresh ) {
|
2021-02-04 20:31:28 +00:00
|
|
|
wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function() {
|
2019-01-23 21:29:55 +00:00
|
|
|
refresh_cart_fragment();
|
|
|
|
} );
|
|
|
|
}
|
2013-12-04 19:15:24 +00:00
|
|
|
});
|