Building Private WooCommerce Shop for Registered Users in WordPress

WooCommerce shop and products are public by default, allowing any user to purchase the products. However, there are occasions where we need to build private WooCommerce shops to keep certain products accessible only to certain type of users. In this tutorial, we are going to look at the process of making the WooCommerce shop private and give access only to the registered users who are logged in to the site. 

There are several free and premium plugins that allows you to build simple and advanced private WooCommerce stores. However, we are going to make the shop private without using any third-party plugins.

How to make the shop private?

First, we have to identify the WooCommerce sections that are part of the shop and product purchasing process. Following are the pages that related to WooCommerce shop.

  • Shop – this is a custom page that acts as the WooCommerce shop. The available products are listed on this page.
  • Product – this is a product detail page where we can see the complete details of the product.
  • Checkout – this page is used to show order details and allow customer to add the billing, shipping and payment details to place the order.
  • Cart – this page acts as the shopping cart where the selected products to be purchased are stored temporarily until you place the order.

In order to make the shop private in WooCommerce, we need to block access to these 4 pages for anyone who is not logged into the site.

Restricting the shop to registered users

We are going to use existing WordPress filters and WooCommerce functions to set the shop as private and give access to the logged in users. You can add the code to the functions.php file of your theme or to a custom plugin. We are going to use a custom plugin as it’s the recommended way of adding custom functionality to WordPress.  If you are not familiar with creating WordPress custom plugins, use the following video to create a plugin and install it.

Now, add the following code to the main plugin file and save the changes.  
function wppcp_redirect_guest_users() {
  if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    if (! is_user_logged_in() && ( is_checkout() || is_cart() || is_woocommerce())) {
      wp_safe_redirect( get_permalink(get_option('woocommerce_myaccount_page_id') );
      exit;
    }
  }
}
add_action( 'template_redirect', 'wppcp_redirect_guest_users' );

Now, the logged in users will be allowed to access the products in private WooCommerce shop. The users who are not logged into the site will be redirected to WooCommerce My Account page with the login form. Once the user is logged in, they can access the store and products.

How it works?

Let’s take a look at the code and how it works using a step by step process.
  1. First, we use the WordPress template_redirect action to intercept the request from user. this action is executed by WordPress before deciding which template to load. By using this action, we can change the default template or completely redirect the user to a new URL.
  2. Inside the function, we check if WooCommerce plugin is activated by using the recommended code by WooCommerce.
  3.  The first condition of next if statement check if the user is logged into the site by using the is_user_logged_in function.
  4. The next 3 WooCommerce conditional functions checks if the user is trying to access a part of our private store.
  5. is_cart and is_checkout functions checks if the user is trying to access shopping cart or the checkout screen of WooCommerce. is_woocommerce function checks if the user is trying to access WooCommerce specific template.
  6. Cart, Checkout and MyAccount are standard WordPRess pages with WooCommerce shortcodes. Therefore, is_woocommerce function cannot be used to validate any of those 3 pages.
  7. If the user is not logged in and trying to access WooCommerce page related to shop, the user is redirected to WooCommerce my account page.

Conclusion

In this tutorial, we used a simple custom code to make WooCommerce shop private and only allow access to the registered users. In real world implementations, you may need to handle advanced scenarios such as making the shop private or certain products private for different type of users. In such cases, you can modify this script to include additional conditions or use an advanced plugin to handle the requirement without any development.

Leave a Reply

Your email address will not be published. Required fields are marked *