1. Knowledge Base
  2. WooCommerce Protected Categories
  3. Developer Documentation

How to check if a category is protected using PHP

The plugin provides a WC_PPC_Visibility class which you can use to check the various visibility settings. You can find the full API for this class here.

To use this class, first create it by passing the product category ID:

$my_category = new WC_PPC_Category_Visibility( 34 );

Then, use one of the methods provided to check whether it is public, protected, or private:

if ( $my_category->is_public() ) {
     // category is public
}

if ( $my_category->is_protected() ) {
    // category is protected - this could be password, role or user protected
}

if ( $my_category->is_password_protected() ) {
    // category is password protected
}

if ( $my_category->is_user_protected() ) {
    // category is user protected
}

if ( $my_category->is_role_protected() ) {
    // category is role protected
}

if ( $my_category->is_private() ) {
    // category is private
}

Each of these methods also accepts an optional $check_ancestors flag (defaults to false). If you set this to true, the function will check any ancestors of the current category as well:

if ( $my_category->is_password_protected( true ) ) {
   // this category or one of its ancestors is password protected
}

To check whether a protected category has been unlocked, use the is_unlocked() function:

if ( $my_category->is_unlocked() ) { 
    // category is unlocked 
}

Still need help?

If searching the knowledge base hasn't answered your question, please contact support.

Related Articles