Last active
July 25, 2019 09:58
-
-
Save GeekPress/9d124b4694535454677fcebb2ee66b40 to your computer and use it in GitHub Desktop.
Cache WooCommerce Get Refreshed Fragment Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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