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

0

Sort order items by SKU

Added: 1 year ago

Last Updated: 1 year ago

Sort the ordered items in woocommerce order by the SKU (as opposed to the order in which the user added the product to the cart)

WooCommerce

AI Verified

0

WooCommerce Show Number Of Products Purchased In Emails

Added: 1 year ago

Last Updated: 1 year ago

This snippet counts the total number of products purchased in the order and outputs the total below the order details table. add_action( 'woocommerce_email_after_orde...

WooCommerce

AI Verified

0

WooCommerce Show Sale Price Dates On Product Page

Added: 1 year ago

Last Updated: 1 year ago

This snippet allows you to show the date range a product is on sale if you had set a sale price in the editor.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

29

Remove Unused Javascript

Added: 1 year ago

Last Updated: 3 weeks ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

9

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 5 months ago

Deactivate some WooCommerce Checkout Fields from showing

Performance

Pro Verified

5

Disable e-icons from Elementor

Added: 1 year ago

Last Updated: 1 year ago

Disable e-icons - and remove the notification from occurring during your Page Speed Insight Check.