October 18, 2023

Restrict Pages with WooCommerce Product Purchase Using Code Snippets

Play Video

To restrict access to specific pages on your WordPress site until a WooCommerce product is purchased, you can use the Code Snippets plugin. This tutorial will walk you through the process of setting up this restriction, ensuring that only those who have made a purchase can access certain content.


Prerequisites

Please make sure you have the following installed and activated:


Identifying Product and Page IDs

  1. Find the Product ID
    Hover over your WooCommerce product in the dashboard to find the product ID. Note it down.
  2. Get the Page Slug
    Navigate to the page you want to restrict and click on ‘Quick Edit’ to find the slug. Note this down as well.

Creating a New Snippet

  1. Navigate to Snippets > Add New and name it “Restrict Pages with WooCommerce”.
  2. Copy the Code:
    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

  3. Modify the Code
    Replace the placeholder IDs and slugs in the code with the ones you noted down earlier.

Additional Products and Pages

If you want to restrict multiple products or pages, you can extend the code snippet to include additional IDs and slugs.


Restrict Pages with WooCommerce Product Purchase – Conclusion

Restricting page access based on WooCommerce product purchases is a straightforward process with the Code Snippets plugin. By following these steps, you can easily set up a system where only verified purchasers can access specific content.

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
Restrict Pages with WooCommerce Product Purchase Using Code Snippets
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.