2015-06-10 17:26:21 +00:00
|
|
|
/* global wc_cart_fragments_params */
|
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 */
|
|
|
|
var $supports_html5_storage;
|
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' );
|
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-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',
|
|
|
|
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 ) );
|
2014-03-18 02:56:31 +00:00
|
|
|
sessionStorage.setItem( 'wc_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
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-10 17:26:21 +00:00
|
|
|
/* Cart Handling */
|
2013-02-26 12:15:12 +00:00
|
|
|
if ( $supports_html5_storage ) {
|
|
|
|
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).bind( 'added_to_cart', function( event, fragments, cart_hash ) {
|
2015-10-06 15:54:06 +00:00
|
|
|
var prev_cart_hash = sessionStorage.getItem( 'wc_cart_hash' );
|
|
|
|
|
|
|
|
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 ) );
|
2014-03-18 02:56:31 +00:00
|
|
|
sessionStorage.setItem( 'wc_cart_hash', cart_hash );
|
2013-02-26 12:15:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2014-03-18 02:56:31 +00:00
|
|
|
var wc_fragments = $.parseJSON( sessionStorage.getItem( wc_cart_fragments_params.fragment_name ) ),
|
|
|
|
cart_hash = sessionStorage.getItem( 'wc_cart_hash' ),
|
2015-10-06 16:04:21 +00:00
|
|
|
cookie_hash = $.cookie( 'woocommerce_cart_hash'),
|
|
|
|
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-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 ) {
|
2013-07-30 14:23:08 +00:00
|
|
|
$.ajax( $fragment_refresh );
|
2013-02-26 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$.ajax( $fragment_refresh );
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:26:21 +00:00
|
|
|
/* Cart Hiding */
|
2014-03-18 02:56:31 +00:00
|
|
|
if ( $.cookie( 'woocommerce_items_in_cart' ) > 0 ) {
|
|
|
|
$( '.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
|
|
|
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).bind( '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
|
|
|
});
|
2013-12-04 19:15:24 +00:00
|
|
|
});
|