How to limit access to Woocommerce during development

This is a quick and dirty solution. Drop it into the top of the functions.php file in your WordPress theme and it will search the page url looking for the strings in the array and redirect anyone who is not logged in as an administrator.

You need to add in any other strings that will identify page urls you want to redirect. Oh and this may not work for everyone.


// if the user is not logged in as an admin
if ( ! current_user_can( 'manage_options' ) ) 
{
        // list of strings to look for in the url
	$urlStringsToBlock = array(
		"/shop/",
		"/product-category/",
		"/product/",
		"/cart/",
		"/checkout/",
		"/stripe-checkout-result/",
		"/my-account/"
	);
	foreach($urlStringsToBlock as $v)
	{
                // loop through and if string is found then redirect
		if(substr_count($_SERVER['REQUEST_URI'], $v)>0)
		{
			header("Location: https://".$_SERVER['HTTP_HOST']);	
			exit();
		}		
	}
}