• Resolved thoseitguys

    (@thoseitguys)


    Is it possible to restrict access to certain categories using their profile post codes?

Viewing 15 replies - 1 through 15 (of 31 total)
  • crslz

    (@crslz)

    Yes you can. I also assume that you want to restrict access for users who are not logged in (guests) whose zip code is unknown for those pages?

    function my_restrict_access() {
        $category = get_queried_object();
    	
        if ( $category ) {
            $category_id = isset( $category->term_id );
    		
            // uncomment line below for debug purposes, this will show you the category id on the current category page
            // echo $category_id;
    	
            if( $category_id == 15 ) {
                if ( !is_user_logged_in() ) {
                    wp_redirect( '/' );
                    exit;
                } 
                else {
                    global $woocommerce;
                    $customer = new WC_Customer();
                    $customer_postcode = $woocommerce->customer->get_billing_postcode();
    				
                    // uncomment line below for debug purposes, this will show you the postcode on the current page
    		// echo $customer_postcode;
    				
    		if ( $customer_postcode == 9000 ) {
    		    wp_redirect( '/' );
    		    exit;
                    }
                }
            }
        }
    }
    add_action( 'template_redirect', 'my_restrict_access');
    Thread Starter thoseitguys

    (@thoseitguys)

    Hi there @crslz thank you so much for the reply.

    Im looking to block ALL postal codes from a certain woocommerce category:

    Except these: 3610, 3626, 3650

    crslz

    (@crslz)

    Hi,

    When validating on the post code, you can add an array with allowed post codes, and then adjust the validation accordingly

    https://www.php.net/manual/en/function.in-array.php

    function my_restrict_access() {
        $category = get_queried_object();
    	
        if ( $category ) {
            $category_id = isset( $category->term_id );
    		
            // uncomment line below for debug purposes, this will show you the category id on the current category page
            // echo $category_id;
    	
            if( $category_id == 15 ) {
                if ( !is_user_logged_in() ) {
                    wp_redirect( '/' );
                    exit;
                } 
                else {
                    global $woocommerce;
                    $customer = new WC_Customer();
                    $customer_postcode = $woocommerce->customer->get_billing_postcode();
    				
                    // uncomment line below for debug purposes, this will show you the postcode on the current page
                    // echo $customer_postcode;
    
                    $allowed_post_code = array(3610, 3626, 3650);
    				
                    if ( !in_array($customer_postcode, $allowed_post_code) ) {
                        wp_redirect( '/' );
                        exit;
                    }
                }
            }
        }
    }
    add_action( 'template_redirect', 'my_restrict_access');

    Regards

    Thread Starter thoseitguys

    (@thoseitguys)

    Thank you again @crslz I did try the code above ( entered my category ID and redirect url)

    But it doesnt seem to work.

    I assume this needs to be added to my funcitons file? Which I have tried.

    Once again thank you for the assistance.

    crslz

    (@crslz)

    1) copy/paste my code to functions.php
    2) uncomment the following rules

    //echo $category_id;

    &

    // echo $customer_postcode;

    3) post the result of the output here (sreenshot) or link to one of your category pages

    Thread Starter thoseitguys

    (@thoseitguys)

    Hi there I have added the code

    Please go to

    http://www.hillcrestkwikspar.co.za/online-store

    on the left sidebar at the bottom is the X Fresh Category that i wish to restrict to those 3 postal codes only.

    crslz

    (@crslz)

    okay, and what’s the category id of that page? I don’t see the output printed on that page anywhere?

    Thread Starter thoseitguys

    (@thoseitguys)

    I also noticed that. I am not sure why it has no output. The Category ID is 3078

    crslz

    (@crslz)

    Can you try the following steps?

    Step 1) Copy/paste this code, execute and view the page

    function my_restrict_access() {
        echo 'step 1 works!';
    }
    add_action( 'template_redirect', 'my_restrict_access');

    Step 2) If step 1 works, replace the code with the one from step 2

    function my_restrict_access() {
        $category = get_queried_object();
    	
        if ( $category ) {
            echo 'step 2 works!';
        } else {
            echo 'step 2 NOT works!';
        }
    }
    add_action( 'template_redirect', 'my_restrict_access');

    Step 3) If step 2 works, perform step 3… replace code with the one from step 3

    function my_restrict_access() {
        $category = get_queried_object();
    	
        if ( $category ) {
            $category_id = isset( $category->term_id );
            if( $category_id == 3078 ) {
                echo 'step 3 works!';
            } else {
                echo 'step 3 NOT works!';
            }
        }
    }
    add_action( 'template_redirect', 'my_restrict_access');

    Up to which step does the code work without problems?

    Thread Starter thoseitguys

    (@thoseitguys)

    Hi @crslz there sorry for the delay.

    Step 1 Works

    Step 2 Works

    its at step 3 where it says not working.

    I did confirm that the category ID is 3078.

    Hope this helps.

    All I want it to do is not open the category and pop a message to the user.

    crslz

    (@crslz)

    Okay, good job! something goes wrong with the category id, can you try the next piece of code and post the output here?

    Regards

    function my_restrict_access() {		
        if ( is_product_category() ) {
            $category = get_queried_object();
    		
            if ( $category ) {
                echo '<pre>', print_r($category, 1), '</pre>';
    		
                $category_id = $category->term_id;
    		
                echo '1 = ' . $category_id . '<br>';
            }
        }
    }
    add_action( 'template_redirect', 'my_restrict_access');
    • This reply was modified 6 years ago by crslz.
    Thread Starter thoseitguys

    (@thoseitguys)

    Gives me a critical error on my site. @crslz

    crslz

    (@crslz)

    Thread Starter thoseitguys

    (@thoseitguys)

    This is the output I get @crslz

    WP_Term Object
    (
    [term_id] => 3078
    [name] => X Fresh Local Deliveries to the Upper Highway only
    [slug] => fresh-local-deliveries
    [term_group] => 0
    [term_taxonomy_id] => 3078
    [taxonomy] => product_cat
    [description] =>
    [parent] => 0
    [count] => 0
    [filter] => raw
    )
    1 = 3078

    Also I dont need a redirect. I think to make it easier.

    Is it possible to Hide the category completely if they dont have one of those 3 postal codes?

    crslz

    (@crslz)

    It looks like the category id now works without problems, so you can use this code.

    You can of course always adjust this with other functionalities that meet your wishes.

    Here you will find some examples: https://businessbloomer.com/?s=categorie

    Regards

    function my_restrict_access() {		
        if ( is_product_category() ) {
            $category = get_queried_object();
    		
            if ( $category ) {
    	    $category_id = $category->term_id;
    			
    	    if( $category_id == 3076 ) {
    		if ( !is_user_logged_in() ) {
    		    wp_redirect( '/' );
    		    exit;
    	        } else {
    		    global $woocommerce;
    		    $customer = new WC_Customer();
    		    $customer_postcode = $woocommerce->customer->get_billing_postcode();
    					
    		    // uncomment line below for debug purposes, this will show you the postcode on the current page
    		    //echo $customer_postcode;
    
    		    $allowed_post_code = array(3610, 3626, 3650);
    					
    		    if ( !in_array($customer_postcode, $allowed_post_code) ) {
    			wp_redirect( '/' );
    			exit;
    		    }
    		}
    	    }
            }
        }
    }
    add_action( 'template_redirect', 'my_restrict_access');
Viewing 15 replies - 1 through 15 (of 31 total)

The topic ‘Restrict Access with Post Code’ is closed to new replies.