If you’re running a WooCommerce store, you may want to restrict access to certain pages based on the products a user has purchased. For example, you might offer exclusive content or services to customers who have bought specific products. This tutorial will guide you through the process of restricting access to pages based on a user’s purchase history using WooCommerce and some custom code.
Step-by-Step Guide
1. Overview of the Code
The code below does two main things:
- Checks if a user is logged in and has bought the required product. If not, they are redirected to the login page or the product page.
- Adds a link to the user’s WooCommerce account dashboard showing which pages they can access based on their purchases.
2. Restricting Page Access Based on Product Purchases
First, we need to restrict access to specific pages depending on whether the user has purchased a product. If they haven’t, they will be redirected to either the login page (if not logged in) or the product page (if they haven’t bought the product).
This function works as follows:
- It checks whether the user is logged in.
- If logged in, it verifies if the user has purchased the required product for the page they are trying to access.
- If they haven’t bought the product, they are redirected to the product page.
- If not logged in, the user is redirected to the login page.
3. Showing Accessible Pages on the User’s Dashboard
Next, we’ll show users links to pages they can access based on the products they’ve bought. This is done by adding a list of accessible pages to the user’s WooCommerce dashboard.
This function:
- Retrieves the list of products the user has bought.
- Maps each product to a corresponding page.
- Displays links to these pages on the user’s WooCommerce dashboard.
4. Helper Functions
The two helper functions below handle checking if the user has purchased and paid for a product and retrieving the list of purchased products.
Checking If a Product Is Bought and Paid
Getting the List of Bought Products
How to Implement This Code on Your Existing Website
To implement this functionality on your WooCommerce site, follow the steps below:
1. Add the Code to Your Theme’s Functions.php File
The easiest way to add this custom code is by placing it inside your theme’s functions.php
file. Here’s how:
- Log into your WordPress dashboard.
- Navigate to Appearance > Theme File Editor.
- In the file list on the right, find and click on
functions.php
under your active theme. - Paste the provided code at the end of the file.
- Click Update File to save your changes.
Note: Make sure to back up your site before making any modifications to your theme files.
2. Use a Custom Plugin
If you prefer to keep your theme files untouched, creating a small custom plugin for this functionality is a more modular solution. Here’s how you can do it:
- In your WordPress dashboard, go to Plugins > Add New and click on Create a Plugin (or use a plugin like Code Snippets).
- Create a new plugin file by navigating to
wp-content/plugins/
on your server using an FTP client or file manager. - Create a folder for your custom plugin (e.g.,
sajdoko-restrict-access
) and add a.php
file within it. - Paste the code into this file, starting with a plugin header like:
- Once you’ve saved the file, navigate back to the WordPress dashboard and activate your new plugin under the Plugins section.
3. Test the Functionality
After implementing the code, test it by logging into your website with a user account that has and hasn’t purchased the required products. Try accessing restricted pages and verify that users are properly redirected based on their purchase status. Also, check the user’s WooCommerce account dashboard to ensure the correct links appear for purchased products.
Make sure to clear your site’s cache if you’re using a caching plugin to see the changes immediately.