Skip to content

Commit

Permalink
Merge pull request woocommerce#9298 from woothemes/issue/8804-reload-…
Browse files Browse the repository at this point in the history
…mini-cart-refresh-nonces

Refresh mini cart fragment before nonces expire.
  • Loading branch information
mikejolley committed Oct 7, 2015
2 parents 3f0cb47 + d04dac6 commit 1a081a9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
50 changes: 47 additions & 3 deletions assets/js/frontend/cart-fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ jQuery( function( $ ) {
$supports_html5_storage = false;
}

/* 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() );
}
}

var $fragment_refresh = {
url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_refreshed_fragments' ),
type: 'POST',
Expand All @@ -30,25 +37,49 @@ jQuery( function( $ ) {
if ( $supports_html5_storage ) {
sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) );
sessionStorage.setItem( 'wc_cart_hash', data.cart_hash );

if ( data.cart_hash ) {
set_cart_creation_timestamp();
}
}

$( document.body ).trigger( 'wc_fragments_refreshed' );
}
}
};

/* Named callback for refreshing cart fragment */
function refresh_cart_fragment() {
$.ajax( $fragment_refresh );
}

/* Cart Handling */
if ( $supports_html5_storage ) {

var cart_timeout = null,
day_in_ms = ( 24 * 60 * 60 * 1000 );

$( document.body ).bind( 'added_to_cart', function( event, fragments, cart_hash ) {
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();
}

sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( fragments ) );
sessionStorage.setItem( 'wc_cart_hash', cart_hash );
});

$( document.body ).bind( 'wc_fragments_refreshed', function() {
clearTimeout( cart_timeout );
cart_timeout = setTimeout( refresh_cart_fragment, day_in_ms );
} );

try {
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' );
cookie_hash = $.cookie( 'woocommerce_cart_hash'),
cart_created = sessionStorage.getItem( 'wc_cart_created' );

if ( cart_hash === null || cart_hash === undefined || cart_hash === '' ) {
cart_hash = '';
Expand All @@ -58,6 +89,19 @@ jQuery( function( $ ) {
cookie_hash = '';
}

if ( cart_hash && ( cart_created === null || cart_created === undefined || cart_created === '' ) ) {
throw 'No cart_created';
}

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 ) );
}

if ( wc_fragments && wc_fragments['div.widget_shopping_cart_content'] && cart_hash === cookie_hash ) {

$.each( wc_fragments, function( key, value ) {
Expand All @@ -70,11 +114,11 @@ jQuery( function( $ ) {
}

} catch( err ) {
$.ajax( $fragment_refresh );
refresh_cart_fragment();
}

} else {
$.ajax( $fragment_refresh );
refresh_cart_fragment();
}

/* Cart Hiding */
Expand Down
2 changes: 1 addition & 1 deletion assets/js/frontend/cart-fragments.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1a081a9

Please sign in to comment.