Link to home
Start Free TrialLog in
Avatar of sharingsunshine
sharingsunshineFlag for United States of America

asked on

Need Help Deleting WooCommerce Page Cache

I have created a cookie via jquery called 'wantmembership', my tests work and display the correct message if the cookie is present.  With the exception, if someone is a guest the message doesn't go away.  This is even after deleting the cookie and clearing the cache.  

I am using FF and I cleared the browser cache via FF.  However reading further, this seems to be why the message is still showing up.

As a result, your cookie may only work as expected when logged in to the WordPress Admin Dashboard. This is because logged-in user sessions specifically bypass the page cache layer, and will be processed by PHP every time.

Open in new window


This is a XAMPP localhost test site and there are no cache plugins installed.

I am running WooCommerce 3.5.4 and WordPress 4.9.8.  This function writes in the Order Notes box 'I Want Membership' if they have pressed the button for membership.

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
	if (isset($_COOKIE['wantmembership'])) {
    $fields['order']['order_comments']['default'] = 'I Want Membership';
	}
    return $fields ;	
}

Open in new window


This is the jquery that creates the cookie

<script type="text/javascript">

jQuery(function() {
	jQuery('#result').text(listCookies);
	jQuery('#cook').click(function(e) {
		e.preventDefault();
		document.cookie = "wantmembership=1; path=/";
		jQuery('#result').text(listCookies);
	});
	
	jQuery('#uncook').click(function(e) {
		e.preventDefault();
		deleteCookie('wantmembership');
		jQuery('#result').text(listCookies);
	});
	
	function deleteCookie(cname) 
	{
		var d = new Date(); //Create an date object
		d.setTime(d.getTime() - (1000*60*60*24)); //Set the time to the past. 1000 milliseonds = 1 second
		var expires = "expires=" + d.toGMTString(); //Compose the expirartion date
		window.document.cookie = cname+"="+"; "+expires;//Set the cookie with name and the expiration date
	}	
	
	function listCookies() {
		var theCookies = document.cookie.split(';');
		var aString = '';
		for (var i = 1 ; i <= theCookies.length; i++) {
			aString += i + ' ' + theCookies[i-1] + "n";
		}
		return aString;
	}	
});

</script>

Open in new window


I need the message to go away if the cookie isn't present for logged in users and guest alike.

Thanks,
Avatar of David Favor
David Favor
Flag of United States of America image

A better way to approach this is to use one of the many Switch User plugins.

The plugins allow switching to any other user + doing an actual login, so you see exactly what a non-admin users sees.

Then you can choose the Switch User Tab to toggle/switch between any number of users for which you have running sessions.

This simple little plugin is worth it's weight in gold.

Tip: Or you can always login as another user in an incognito tab/window + accomplish the same behavior.
Avatar of sharingsunshine

ASKER

Looks like the plugin could be helpful but it doesn't answer my question.  The login isn't the problem, this is the issue:

I need the message to go away if the cookie isn't present for logged in users and guest alike.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sharingsunshine
sharingsunshine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial