Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Last active March 5, 2026 09:37
Show Gist options
  • Select an option

  • Save xlplugins/9b3ed1d02659b8be7396e3963e99f902 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/9b3ed1d02659b8be7396e3963e99f902 to your computer and use it in GitHub Desktop.
refresh checkout on session expiry
/**
* If update_order_review returns and woocommerce_cart_hash cookie is not found → refresh checkout once.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'wp_footer', 'keep_wc_session_refresh_script', 20 );
function keep_wc_session_refresh_script() {
if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || is_wc_endpoint_url( 'order-received' ) ) {
return;
}
?>
<script>
(function($) {
var done = 'wc_checkout_refresh_done';
$(document).ajaxComplete(function(e, xhr, s) {
if ( sessionStorage.getItem(done) === '1' ) return;
if ( ! s || ! s.url || s.url.indexOf('update_order_review') === -1 ) return;
var m = document.cookie.match( /(?:^|; )woocommerce_cart_hash=([^;]*)/ );
if ( ! m || ! m[1] ) {
sessionStorage.setItem(done, '1');
window.location.reload();
}
});
})(jQuery);
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment