Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GeekPress/9d124b4694535454677fcebb2ee66b40 to your computer and use it in GitHub Desktop.
Save GeekPress/9d124b4694535454677fcebb2ee66b40 to your computer and use it in GitHub Desktop.
Cache WooCommerce Get Refreshed Fragment Request
<?php
if ( isset( $_GET['wc-ajax'] ) && 'get_refreshed_fragments' === $_GET['wc-ajax'] && ( !isset( $_COOKIE['woocommerce_cart_hash'] ) || empty( $_COOKIE['woocommerce_cart_hash'] ) ) && ( !isset( $_COOKIE['woocommerce_items_in_cart'] ) || empty( $_COOKIE['woocommerce_items_in_cart'] ) ) ) {
$data = get_transient( 'rocket_get_refreshed_fragments_cache' );
if ( ! empty( $data ) ) {
$data = json_decode( $data );
wp_send_json( $data );
}
ob_start();
add_action( 'shutdown', function() {
set_transient( 'rocket_get_refreshed_fragments_cache', ob_get_clean(), 7 * DAY_IN_SECONDS );
}, 0 );
}
// Delete the transient when the current theme is changed.
add_action( 'switch_theme', function() {
delete_transient( 'rocket_get_refreshed_fragments_cache' );
}, 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment