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 hide product summary

Added: 1 year ago

Last Updated: 1 year ago

Hides the product summary in single product globally

WooCommerce

AI Verified

0

Streamline Your Shopping with Automatic Coupon Code Integration in Your Cart

Added: 7 months ago

Last Updated: 7 months ago

Enhance your shopping experience with seamless savings! We believe in making your store user-friendly to boost order satisfaction. Say goodbye to manual coupon entries – our system now automatically a...

WooCommerce

AI Verified

0

WooCommerce Change Variable Product Price Range Display

Added: 1 year ago

Last Updated: 1 year ago

If you wish to customise how variable product prices are displayed on both the Product Archive and Single Product templates, then this guide is for you. In this example, we remove the dash between the...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

31

Remove Unused Javascript

Added: 1 year ago

Last Updated: 4 weeks 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: 2 days ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 7 months ago

Last Updated: 1 month ago

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