Support » Plugin: WP Super Cache » Excluding specific cookies such as woocommerce_cart_hash

  • Hi there,

    I am trying to stop caching on the Woocommerce cart cookie woocommerce_cart_hash to stop previous website pages from resetting the cart to 0 after a product has been added. Please advise.

    Cheers,
    Chad

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Donncha Ó Caoimh

    (@donncha)

    You need to write a Supercache plugin that looks for the woocommerce_cart_hash and then sets the DONOTCACHEPAGE constant, or adds that cookie to the list of cookies used by the plugin as an index to the cache. Look for the “wp_cache_get_cookies_values” action in plugins/searchengine.php for code that does something similar.

    Here’s a page describing how to write such a plugin:

    https://github.com/Automattic/wp-super-cache/wiki/Writing-WP-Super-Cache-Plugins

    Thread Starter quantum17

    (@quantum17)

    Hi Donncha, thanks for the info. If I was to add the following code to wp-config.php would this work?

    if(!empty($_COOKIE)) foreach($_COOKIE as $__name => $__value)
        if(stripos($__name, 'woocommerce_cart_hash') !== FALSE)
            { define('DONOTCACHEPAGE', TRUE); break; }
    unset($__name, $__value);

    Hi @quantum17

    You should create simple must-use plugin (or use child theme). It’s much better than adding code into ‘wp-config.php’.

    And you can write it into one line (which is faster):
    isset( $_COOKIE['woocommerce_cart_hash'] ) && define( 'DONOTCACHEPAGE', true );

    Thread Starter quantum17

    (@quantum17)

    Thanks for your reply Sasa, I’m very basic with php so do I just put the following in a php file?

    <?php 
    isset( $_COOKIE['woocommerce_cart_hash'] ) && define( 'DONOTCACHEPAGE', true );
    ?>
    

    Would greatly appreciate your help.

    You should create simple PHP file (if you want to create your first plugin 🙂 ):

    <?php
    /**
     * Plugin Name: My first plugin
     * Version: 1.0.0
     * Plugin URI:
     * Description: Prevent caching if cookie ...
     * Author:
     * Author URI:
     * License: GPL v3
     */
    
    if( !defined( 'ABSPATH') ) exit();
    
    isset( $_COOKIE['woocommerce_cart_hash'] ) && define( 'DONOTCACHEPAGE', true );
    

    You can replace Plugin Name and Description to something else. You will see it when you go on WP dashboard/plugins under Must-use plugins. Don’t put closing PHP tag ?> at the end of file because it can make unwanted ‘white-scape’ in HTML code.

    Then just upload it in wp-content/mu-plugins/ (Create this directory if it doesn’t exist). You can choice any filename.

    Also, it’s possible to add more code there if you need it.

    I hope that helps,
    Sasa

    • This reply was modified 3 years, 10 months ago by Saša.
    Thread Starter quantum17

    (@quantum17)

    Thanks Sasa, really appreciate your help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Excluding specific cookies such as woocommerce_cart_hash’ is closed to new replies.