Cache Cart Fragements

Created by WP Fix It, Modified on Mon, 5 Nov, 2018 at 3:44 PM by WP Fix It

 You can put this file into wp-content/mu-plugins folder.


See full details at https://gist.github.com/GeekPress/9d124b4694535454677fcebb2ee66b40


<?php
if ( isset( $_GET['wc-ajax'] ) &amp;amp;amp;&amp;amp;amp; 'get_refreshed_fragments' === $_GET['wc-ajax'] &amp;amp;amp;&amp;amp;amp; ( !isset( $_COOKIE['woocommerce_cart_hash'] ) || empty( $_COOKIE['woocommerce_cart_hash'] ) ) &amp;amp;amp;&amp;amp;amp; ( !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 );


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article