Warm tip: This article is reproduced from stackoverflow.com, please click
woocommerce wordpress

Pop up Message when clicking Woocommerce Categories

发布于 2020-03-31 23:02:10

I want a pop up message to appear when a user without a certain postal code tries to look at a certain category.

I have gotten as far as to redirect the user if there postal code is not in the allowed array.

I would just like it to state "You cannot access this category" and without any redirect.

function my_restrict_access() {     
if ( is_product_category() ) {
    $category = get_queried_object();

    if ( $category ) {
    $category_id = $category->term_id;

    $allowed_category_id = array(3078, 3385);

    if ( in_array($category_id, $allowed_category_id) ) {
    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');
Questioner
Jürgen Walter Hof
Viewed
78
Sajjad Hossain Sagor 2020-01-31 20:17

You better need to use wp filter the_content to change front content...

add_action( 'the_content', 'restrict_user_to_certain_shop_category' );

function restrict_user_to_certain_shop_category( $content ){

   global $woocommerce;

   // list of all allowed woo product categories
   $allowed_categories = array( 3078, 3385 );

   // allowed user whose postcodes are following
   $allowed_post_codes = array( 3610, 3626, 3650 );

   // check if current viweing page is product archive page
   if ( is_product_category() ) {

      $current_category = get_queried_object();

      // extract archive category id
      $current_category_id = $current_category->term_id;

      // check if current category is in allowed array list
      if ( in_array( $current_category_id, $allowed_categories ) ) {

          // check if user is not logged in
          if ( ! is_user_logged_in() ) {

              // message to show when user is not logged in and viewing allowed product category
              $content = 'You cannot access this category';
          }

          // user is logged in and viewing allowed product category
          else {

              $customer = new WC_Customer();

              $customer_postcode = $woocommerce->customer->get_billing_postcode();

              // check if user is from allowed postarea
              if ( !in_array( $customer_postcode, $allowed_post_codes ) ) {

                   // message to show when user is logged in and viewing allowed product category + from allowed postarea
                  $content = 'You cannot access this category';
              }
           }
        }
      }

   return $content;
}