2014-01-31 02:44:17 +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
|
|
|
|
if (typeof wc_cart_fragments_params === "undefined")
|
|
|
|
return false;
|
|
|
|
|
2013-02-26 12:15:12 +00:00
|
|
|
/** Cart Handling */
|
|
|
|
$supports_html5_storage = ( 'sessionStorage' in window && window['sessionStorage'] !== null );
|
|
|
|
|
|
|
|
$fragment_refresh = {
|
2013-06-11 12:31:41 +00:00
|
|
|
url: wc_cart_fragments_params.ajax_url,
|
2013-02-26 12:15:12 +00:00
|
|
|
type: 'POST',
|
2013-11-19 13:44:28 +00:00
|
|
|
data: { action: 'woocommerce_get_refreshed_fragments' },
|
2013-02-26 12:15:12 +00:00
|
|
|
success: function( data ) {
|
|
|
|
if ( data && data.fragments ) {
|
|
|
|
|
|
|
|
$.each( data.fragments, function( key, value ) {
|
|
|
|
$(key).replaceWith(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
if ( $supports_html5_storage ) {
|
2013-11-20 16:31:28 +00:00
|
|
|
sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) );
|
2013-02-26 12:15:12 +00:00
|
|
|
sessionStorage.setItem( "wc_cart_hash", data.cart_hash );
|
|
|
|
}
|
|
|
|
|
2013-04-23 14:37:19 +00:00
|
|
|
$('body').trigger( 'wc_fragments_refreshed' );
|
2013-02-26 12:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( $supports_html5_storage ) {
|
|
|
|
|
|
|
|
$('body').bind( 'added_to_cart', function( event, fragments, cart_hash ) {
|
2013-11-20 16:31:28 +00:00
|
|
|
sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( fragments ) );
|
2013-02-26 12:15:12 +00:00
|
|
|
sessionStorage.setItem( "wc_cart_hash", cart_hash );
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2013-11-20 16:31:28 +00:00
|
|
|
var wc_fragments = $.parseJSON( sessionStorage.getItem( wc_cart_fragments_params.fragment_name ) );
|
2013-02-26 12:15:12 +00:00
|
|
|
var cart_hash = sessionStorage.getItem( "wc_cart_hash" );
|
2013-07-30 14:23:08 +00:00
|
|
|
var cookie_hash = $.cookie( "woocommerce_cart_hash" );
|
2013-02-26 12:15:12 +00:00
|
|
|
|
2013-07-30 14:23:08 +00:00
|
|
|
if ( cart_hash == null || cart_hash == undefined || cart_hash == '' )
|
|
|
|
cart_hash = '';
|
|
|
|
|
|
|
|
if ( cookie_hash == null || cookie_hash == undefined || cookie_hash == '' )
|
|
|
|
cookie_hash = '';
|
|
|
|
|
|
|
|
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 ) {
|
|
|
|
$(key).replaceWith(value);
|
|
|
|
});
|
|
|
|
|
2013-04-23 14:37:19 +00:00
|
|
|
$('body').trigger( 'wc_fragments_loaded' );
|
|
|
|
|
2013-02-26 12:15:12 +00:00
|
|
|
} else {
|
|
|
|
throw "No fragment";
|
|
|
|
}
|
|
|
|
|
|
|
|
} 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 );
|
|
|
|
}
|
|
|
|
|
2013-04-08 17:43:07 +00:00
|
|
|
/* Cart hiding */
|
|
|
|
if ( $.cookie( "woocommerce_items_in_cart" ) > 0 )
|
2013-04-09 08:38:55 +00:00
|
|
|
$('.hide_cart_widget_if_empty').closest('.widget_shopping_cart').show();
|
2013-04-08 17:43:07 +00:00
|
|
|
else
|
2013-04-09 08:38:55 +00:00
|
|
|
$('.hide_cart_widget_if_empty').closest('.widget_shopping_cart').hide();
|
|
|
|
|
|
|
|
$('body').bind( 'adding_to_cart', function() {
|
|
|
|
$('.hide_cart_widget_if_empty').closest('.widget_shopping_cart').show();
|
|
|
|
} );
|
2013-04-08 17:43:07 +00:00
|
|
|
|
2013-12-04 19:15:24 +00:00
|
|
|
});
|