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
|
|
|
|
2013-02-26 12:15:12 +00:00
|
|
|
/** Cart Handling */
|
2014-04-07 13:34:47 +00:00
|
|
|
try {
|
|
|
|
$supports_html5_storage = ( 'sessionStorage' in window && window.sessionStorage !== null );
|
|
|
|
} catch( err ) {
|
|
|
|
$supports_html5_storage = false;
|
|
|
|
}
|
2013-02-26 12:15:12 +00:00
|
|
|
|
|
|
|
$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 ) {
|
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 );
|
2013-02-26 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 02:56:31 +00:00
|
|
|
$( 'body' ).trigger( 'wc_fragments_refreshed' );
|
2013-02-26 12:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( $supports_html5_storage ) {
|
|
|
|
|
2014-03-18 02:56:31 +00:00
|
|
|
$( '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 ) );
|
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' ),
|
|
|
|
cookie_hash = $.cookie( 'woocommerce_cart_hash' );
|
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
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2014-03-18 02:56:31 +00:00
|
|
|
$( 'body' ).trigger( 'wc_fragments_loaded' );
|
2013-04-23 14:37:19 +00:00
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2013-04-08 17:43:07 +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
|
|
|
|
2014-03-18 02:56:31 +00:00
|
|
|
$( 'body' ).bind( 'adding_to_cart', function() {
|
|
|
|
$( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
|
2013-04-09 08:38:55 +00:00
|
|
|
} );
|
2013-04-08 17:43:07 +00:00
|
|
|
|
2013-12-04 19:15:24 +00:00
|
|
|
});
|