• Resolved gbruzzo

    (@gbruzzo)


    Hello there,
    thank you for the great plugin.

    Wordpress 4.6.1 / Woocommerce 2.6.4

    I am experiencing a crucial issue with the plugin, hoping you can help.
    The plugin currently caches my cart, which is in a dedicated widget position on every page (cart)
    The code in the widget position is :

    <?php 
    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
     
        $wc = WC();
        $count = $wc->cart->cart_contents_count;
    	if ($count !=0) { 
    	?><a class="cart-contents" href="<?php echo WC()->cart->get_checkout_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo '(' . $count . ')'; ?></a><?php
    			}
        else { 
        	?><a class="cart-contents" title="<?php _e( 'Your Cart is Empty' ); ?>"><?php echo '(0)'; ?></a><?php
    	 }
    } ?>

    The code in the functions.php file is

    add_filter( 'woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment' );
    function my_header_add_to_cart_fragment( $fragments ) {
    	
    
        ob_start();
        $count = WC()->cart->cart_contents_count;
    	if ($count !=0) { 
    	?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View Cart and Checkout' ); ?>"><?php echo '(' . $count . ')'; ?></a><?php
    	}
        else { 
        	?><a class="cart-contents" title="<?php _e( 'Your Cart is Empty' ); ?>"><?php echo '(0)'; ?></a><?php
    	}
     
        $fragments['a.cart-contents'] = ob_get_clean();
         
        return $fragments;
    }
    

    I do not seem able to remove this snippet from page caching; as the user (non-logged in) adds goods to the cart, the cart is updated. Moving to another page in the site results in “(0)” goods appearing again (as the code is not executed, the cached version being called).

    What I need to achieve is for the specific widget position ‘cart’ not to be cached , but I am not succeeding.

    I Tried adding

    define('COMET_CACHE_ALLOWED', FALSE); 
    or $_SERVER['COMET_CACHE_ALLOWED'] = FALSE; 
    or define('DONOTCACHEPAGE', TRUE); 

    to the code snippets above, but without success.

    Could you please advise? It is essential for us. We would love to use your plugin (we are PRO customers).

    Thank you in advance

    • This topic was modified 8 years, 5 months ago by gbruzzo.
Viewing 5 replies - 1 through 5 (of 5 total)
  • @gbruzzo It’s not currently possible to exclude specific portions of the page from being cached — you need to exclude the entire page that includes the cart widget (if that’s every page, then that’s a problem). We have a feature request for Dynamic Fragmentation, but there are lots of security implications with such a feature (see the feature request for details). If you’re interested in seeing this feature added at a future date, I recommend leaving a comment on the feature request to show your vote.

    In the meantime, your only options are to not cache any pages that have the cart widget, or to find a widget that uses JavaScript to update the cart contents (JavaScript does not get cached server-side, so a JS-powered widget would be cache-compatible).

    Thread Starter gbruzzo

    (@gbruzzo)

    Ahhh no, this was not the answer I was hoping to get.

    Do you have any suggestions about possible cart widgets that you know work with your plugin? Our cart is in the navbar, so yes, on every page.

    Given your premise, I do not believe you will ever release an update to cater for a widget specific cache exclusions. Pity

    Can I get a refund?

    Thank you

    Giacomo Bruzzo

    Thread Starter gbruzzo

    (@gbruzzo)

    An update and an apology – the problem was my fault it seems.

    I was dequeuing the javascript from the homepage (any non WC page for that matter).

    
    add_action( 'wp_enqueue_scripts', 'child_manage_woocommerce_styles', 99 );
    
    function child_manage_woocommerce_styles() {
    	//remove generator meta tag
    	remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );
    
    	//first check that woo exists to prevent fatal errors
    	if ( function_exists( 'is_woocommerce' ) ) {
    		//dequeue scripts and styles
    		if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
    			wp_dequeue_style( 'woocommerce_frontend_styles' );
    			wp_dequeue_style( 'woocommerce_fancybox_styles' );
    			wp_dequeue_style( 'woocommerce_chosen_styles' );
    			wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
    			wp_dequeue_script( 'wc_price_slider' );
    			wp_dequeue_script( 'wc-single-product' );
    			wp_dequeue_script( 'wc-add-to-cart' );
    			wp_dequeue_script( 'wc-cart-fragments' );
    			wp_dequeue_script( 'wc-checkout' );
    			wp_dequeue_script( 'wc-add-to-cart-variation' );
    			wp_dequeue_script( 'wc-single-product' );
    			wp_dequeue_script( 'wc-cart' );
    			wp_dequeue_script( 'wc-chosen' );
    			wp_dequeue_script( 'woocommerce' );
    			wp_dequeue_script( 'prettyPhoto' );
    			wp_dequeue_script( 'prettyPhoto-init' );
    			wp_dequeue_script( 'jquery-blockui' );
    			wp_dequeue_script( 'jquery-placeholder' );
    			wp_dequeue_script( 'fancybox' );
    			wp_dequeue_script( 'jqueryui' );
    		}
    	}
    
    }

    Once I let the scripts back in, caching of the cart stops.

    Sorry for that, I will keep using the awesome plugin!

    Giacomo

    @gbruzzo Glad to hear that you solved the issue. ?? Thanks for posting an update here for others to benefit from!

    Hello there,

    Your snippet was helping resolve the problem on my homepage.

    But on other page like category and single product, the cache problem with cart is still there.

    How do I manage to find these value of these pages to add on the list?

    like wc-cart and wc-chosen..etc.

    Thanks a ton!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘caching problem with woocommerce cart widget’ is closed to new replies.