July 17, 2023

Let WooCommerce Restrict Content Access until Purchase Made with a Code Snippet

Play Video

This ‘WooCommerce Restrict Content’ tutorial by Web Squadron puts your content behind closed doors until the user buys the product. With the help of the Code Snippets plugin and a little bit of code, we can make it happen in no time. And don’t worry, you do not have to write the code yourself! Let’s get started.


Understanding the Concept

Think of it like this: you’re running an online course platform. You’ve got a stellar course on “Nautical Navigation” and another on “Outback Survival”. You want to ensure that only those who’ve purchased these courses can access the related content. That’s the goal we’re aiming for here.


WooCommerce Restrict Content Preparation

Before we dive in, we need to identify a couple of things. First, determine the WooCommerce products you want to link to your content. In our case, it’s the “Nautical Navigation” and “Outback Survival” courses. Each product in WooCommerce has a unique ID. For our example, “Nautical Navigation” has an ID of 298 and “Outback Survival” has an ID of 297.

Next, decide which WordPress pages you want to restrict. For our example, we’ll restrict two pages: “Layout” and “Test”. Each page has a unique slug, which is the part of the URL that identifies the page. In our case, the slugs are “layout” and “test”.


The Code Snippet

Now, let’s talk about the magic that makes this all possible: the code snippet. This little piece of code is like the bouncer at the door of your exclusive club, only letting in the folks who’ve bought a ticket (or in our case, a product).

Here’s how it works. The code checks if a user has bought a specific product before they can access a specific page. If they haven’t, they get redirected to another page, like your shop page or a custom “Access Denied” page.


Implementing the ‘WooCommerce Restrict Content’ Code Snippet

Time to get our hands dirty. Here’s how you can add the code snippet to your WordPress site using the Code Snippets plugin:

  1. From your WordPress dashboard, go to Plugins > Add New.
  2. Search for “Code Snippets” and click “Install Now”.
  3. After the plugin is installed, click “Activate”.
  4. Go to Snippets > Add New.
  5. Give your new snippet a title, something like “Restrict Content Access”.
  6. Copy & Paste the code snippet below in your code box and adjust the IDs & slugs accordingly.
    function restrict_page_access() {
        // Get the current user ID
        $user_id = get_current_user_id();
    
        // Check if the user is an administrator
        $is_admin = current_user_can('administrator');
    
        // Check if the user has purchased the WooCommerce product with ID 297
        $has_purchased_297 = wc_customer_bought_product($user_id, $user_id, 297);
    
        // Get the current page
        $current_page = get_queried_object();
    
        // Check if the current page is "layout" and the user is not an administrator
        if ($current_page->post_name === 'layout' && !$has_purchased_297 && !$is_admin) {
            // Redirect the user to a specific URL
            wp_redirect('https://website.websquadron.co.uk/shop');
            exit;
        }
    
        // Check if the current page is "test" and the user is not an administrator
        if ($current_page->post_name === 'test' && !$is_admin) {
            // Check if the user has purchased the WooCommerce product with ID 298
            $has_purchased_298 = wc_customer_bought_product($user_id, $user_id, 298);
            
            // Check if the user has purchased either product 297 or 298
            if (!($has_purchased_297 || $has_purchased_298)) {
                // Redirect the user to a specific URL
                wp_redirect('https://website.websquadron.co.uk/shop');
                exit;
            }
        }
    }
    add_action('template_redirect', 'restrict_page_access');

    Source: Code Snippets

  7. Click “Save Changes and Activate”.

Remember to replace the product IDs and page slugs in the code snippet with your own. Test everything thoroughly to make sure it’s working as expected.


Handling Unauthorized Access

So, what happens when someone tries to sneak into the club without a ticket? Well, our bouncer (the code snippet) sends them packing to a different page. You can set this redirect page to be anything you want. Maybe it’s your shop page, or maybe it’s a custom page that says, “Hey, you need to buy a product to see this content!”


Advanced Usage

Feeling adventurous? You can tweak the code snippet to do even more. For example, you can set it up so that a user needs to purchase one of several products to access a page. Or, you could restrict multiple pages based on different products. The sky’s the limit!


Conclusion

And there you have it! With a bit of code and some creativity, you can control who sees what content on your WordPress site based on their WooCommerce purchases. It’s like having your own personal bouncer, keeping the riff-raff out of your exclusive content club.


Required Resources

WooCommerce Logo
Free Options
Code Snippets Logo
Free Options
Credit to Web Squadron
Web Squadron is a popular YouTube channel dedicated to providing informative tutorials, tips, and insights on web development and design. With a strong focus on WordPress and website builders like Bricks Builder, the channel aims to help users of all skill levels, from beginners to advanced developers, create and optimize their websites. By offering easy-to-understand, step-by-step guidance, Web Squadron provides practical and valuable content.
Visit
Let WooCommerce Restrict Content Access until Purchase Made with a Code Snippet
Welcome back!
Enter your Helwp credentials to sign in.

No Account yet? Sign Up

My Account
Menu
Give Feedback
Describe your feedback *
Rate Helwp
Share
Facebook
Twitter
LinkedIn
Reddit
Email
WhatsApp
Telegram
Pocket
Report
Problem *
Describe the problem
Want us to reply?
Your E-Mail
Affiliate Disclosure

At Helwp, we’re committed to transparency and honesty. Therefore, we want to inform you that some of the links on our website are affiliate links. This means that, at no additional cost to you, we may earn a small commission if you click through and make a purchase.

We only promote products or services that we genuinely believe in. These affiliate commissions help us to maintain the website and continue to provide you with free, high-quality WordPress content.

If you are interested in how you can support us even further, check out our support page.