Need Help Deleting WooCommerce Page Cache

sharingsunshine
sharingsunshine used Ask the Experts™
on
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,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorLinux/LXD/WordPress/Hosting Savant
Distinguished Expert 2018

Commented:
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.

Author

Commented:
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

I decided to go a different way.  The cookie has a value of 1 so instead of deleting it I set it to a value of 0.  When it is zero the message won';t be written again.

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'])) {
	if(isset($_COOKIE['wantmembership']) && $_COOKIE['wantmembership']== 1 ) {
    $fields['order']['order_comments']['default'] = 'I Want Membership';
	}
    return $fields;
		
}

add_action( 'woocommerce_order_status_processing', 'delete_cookie', 10, 1);

function delete_cookie () {
					if(isset($_COOKIE['wantmembership']) && $_COOKIE['wantmembership']== 1 ) {
				echo "found the cookie";
			$cookie_name = 'wantmembership';
				$cookie_value=0;
unset($_COOKIE[$cookie_name]);
// change cookie value
setcookie($cookie_name, $cookie_value);
			
		}
	}

Open in new window


The first function creates the cookie and gives it a value of 1.

The second function is when the order is processing it sets the value of the cookie to 0.  If the customer comes back in without closing their browser they won't see the message because the cookie value has to be 1 to see it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial