• Resolved sillyninja

    (@sillyninja)


    I want to hide few products from Unregistered/non-login users. I am using the following code. I only want to display $cat = 'visible'; to logout users. If a user has the link to the product and tries to open a page (the product or category I have hidden), the user should be redirected to https://www.example.com/redirect.

    But the code hides products from all the categories, and it does not hide the categories either.

    function my_product_query( $q ) {
        // Is user logged in
        $is_logged_in = is_user_logged_in();
    	
        // Category to hide
        $cat = 'visible';
    
        // Not logged in
        if ( !$is_logged_in ) {
            $q->set( 'tax_query',
                array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'testing1,testing2,testing3',
                        'terms' => $cat, // your category slug
                        'operator' => 'NOT IN'
                    )
                )
            );
        }
    }
    add_action( 'woocommerce_product_query', 'my_product_query', 10, 1 );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Ross V. a11n

    (@rossviviano)

    Automattic Happiness Engineer

    Hi there,

    This is a fairly complex development topic (unless you want to use a plugin to achieve this), so I’m going to leave it open for a bit to see if anyone is able to jump in to help you out

    I can also recommend the following places for more development-oriented questions:

    WooCommerce Slack Community: https://woocommerce.com/community-slack/
    Advanced WooCommerce group on Facebook: https://www.facebook.com/groups/advanced.woocommerce/

    Best,

    Ross

    Plugin Support Ross V. a11n

    (@rossviviano)

    Automattic Happiness Engineer

    Hi again,

    It doesn’t look like anyone’s going to chime in here, so I’m going to close this thread. Your best bet would be to get in touch with one of the groups mentioned in my previous response.

    Best,

    Ross

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to hide WooCommerce product page for Unregistered Users?’ is closed to new replies.