AI Verified

Name

Restrict Accessing Pages until Product Purhased

About

Lock away access to Pages until certain WooCommerce Products are purchased,

Language

PHP

Rating

Voted: 1 by 1 user(s)

How to Setup Snippet

Change the Product IDs and the Page Slug names accordingly.

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

16/10/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Restrict Accessing Pages until Product Purhased

 
                    
1function restrict_page_access() {
2 // Get the current user ID
3 $user_id = get_current_user_id();
4 
5 // Check if the user is an administrator
6 $is_admin = current_user_can('administrator');
7 
8 // Check if the user has purchased the WooCommerce product with ID 297
9 $has_purchased_297 = wc_customer_bought_product($user_id, $user_id, 297);
10 
11 // Get the current page
12 $current_page = get_queried_object();
13 
14 // Check if the current page is "layout" and the user is not an administrator
15 if ($current_page->post_name === 'layout' && !$has_purchased_297 && !$is_admin) {
16 // Redirect the user to a specific URL
17 wp_redirect('https://website.websquadron.co.uk/shop');
18 exit;
19 }
20 
21 // Check if the current page is "test" and the user is not an administrator
22 if ($current_page->post_name === 'test' && !$is_admin) {
23 // Check if the user has purchased the WooCommerce product with ID 298
24 $has_purchased_298 = wc_customer_bought_product($user_id, $user_id, 298);
25 
26 // Check if the user has purchased either product 297 or 298
27 if (!($has_purchased_297 || $has_purchased_298)) {
28 // Redirect the user to a specific URL
29 wp_redirect('https://website.websquadron.co.uk/shop');
30 exit;
31 }
32 }
33}
34add_action('template_redirect', 'restrict_page_access');

1

Featured Snippet

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

1

WooCommerce Show Custom Text Above Login Form Fields

Added: 1 year ago

Last Updated: 2 weeks ago

This guide shows you how to add custom content above the login form fields shown on the My Account page.

WooCommerce

AI Verified

1

WooCommerce Create Custom Order Statuses

Added: 1 year ago

Last Updated: 3 weeks ago

Creating a custom order status in WooCommerce can be useful if there are extra steps in your business processes. Perhaps you would like to set a custom order status of “Dispatched”, “Exchanged” or “Be...

WooCommerce

AI Verified

0

Learndash 'my courses' tab on woocommerce my account page

Added: 1 year ago

Last Updated: 1 year ago

Add a short cut to the customers courses from his Woocommerce my acount dashboard.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

33

Remove Unused Javascript

Added: 1 year ago

Last Updated: 2 days ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 3 weeks ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 8 months ago

Last Updated: 2 months ago

This can be used for any WordPress Builder to aid working with CSS Grids.