Restrict Multiple Pages Based on Product Purchases

WooCommerce PHP July 23, 2026

Change the product ids and page ids as required. To find page id you can edit an page and in url bar you can see the id.

✅ Shows a styled "Access Denied" message

✅ Hides the header and footer (for a cleaner, locked-down view)

✅ Adds a “Go to Home” button below the message

Code:

add_action('template_redirect', function () {
    if (is_admin() || !is_user_logged_in()) {
        return;
    }
    // Define page IDs and required product IDs
    $restricted_pages = [
        123 => [101, 102],        // Page ID 123 → Product 101 or 102
        456 => [201],             // Page ID 456 → Product 201
        789 => [301, 302, 303],   // Page ID 789 → Product 301, 302 or 303
    ];
    $current_page_id = get_queried_object_id();
    if (!array_key_exists($current_page_id, $restricted_pages)) {
        return;
    }
    $allowed_product_ids = $restricted_pages[$current_page_id];
    $user_id = get_current_user_id();
    $orders = wc_get_orders([
        'customer_id' => $user_id,
        'status' => ['completed', 'processing'],
        'limit' => -1,
    ]);
    $purchased_product_ids = [];
    foreach ($orders as $order) {
        foreach ($order->get_items() as $item) {
            $purchased_product_ids[] = $item->get_product_id();
        }
    }
    $has_access = array_intersect($allowed_product_ids, $purchased_product_ids);
    if (empty($has_access)) {
        // Hook into content and replace it
        add_filter('the_content', function () {
            ob_start();
            ?>
            <div class="access-denied-box">
                <h2>Access Denied</h2>
                <p>You do not have permission to view this page. Please purchase the required product to gain access.</p>
                <a href="<?php echo esc_url(home_url('/')); ?>" class="home-button">Go to Homepage</a>
            </div>
            <style>
                /* Hide header and footer */
                header, footer, .site-header, .site-footer {
                    display: none !important;
                }
                /* Full page box */
                .access-denied-box {
                    border: 2px solid #e74c3c;
                    background-color: #fcebea;
                    color: #c0392b;
                    padding: 30px;
                    margin: 100px auto;
                    max-width: 600px;
                    text-align: center;
                    font-family: sans-serif;
                    border-radius: 8px;
                    box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
                }
                .access-denied-box h2 {
                    margin-top: 0;
                    font-size: 28px;
                }
                .access-denied-box p {
                    font-size: 18px;
                    margin-bottom: 20px;
                }
                .access-denied-box .home-button {
                    display: inline-block;
                    padding: 10px 20px;
                    background-color: #c0392b;
                    color: #fff;
                    text-decoration: none;
                    border-radius: 4px;
                    transition: background 0.3s ease;
                }
                .access-denied-box .home-button:hover {
                    background-color: #a93226;
                }
            </style>
            <?php
            return ob_get_clean();
        });
    }
});