• Resolved klol

    (@klol)


    Hello,
    I hope you are well.

    I found this code on another forum post but unfortunately it doesn’t work for me.

    I want to do that people who are neither Administrator or Anim, can’t see the products of the Anim category in the search.

    If I put it like this, the visitors see the products but not the Administrators or Anim.

    add_filter( 'dgwt/wcas/search_query/args', function ( $args ) {
    
    	$args['tax_query'][] = array(
    		'taxonomy' => 'product_cat',
    		'field'    => 'slug',
    		'terms'    => array( 'animatrices' ),
    		'operator' => 'NOT IN'
    	);
    
    	return $args;
    } );

    I tried to do this:

    add_filter( 'dgwt/wcas/search_query/args', function ( $args ) {
    
    global $woocommerce;
      $user = wp_get_current_user();
            $allowed_roles = array( 'customer','subscriber');
            if (array_intersect( $allowed_roles, $user->roles) || (!is_user_logged_in)) {
    			
    	$args['tax_query'][] = array(
    		'taxonomy' => 'product_cat',
    		'field'    => 'slug',
    		'terms'    => array( 'animatrices' ),
    		'operator' => 'NOT IN'
    	);
    
    	return $args;
    			
    			}
    } );

    But it doesn’t show the products below the categories anymore.

    There is really something I don’t understand.
    Do you have a clue?
    Thank you very much for your help.
    Carole

    • This topic was modified 2 years, 2 months ago by klol.
Viewing 1 replies (of 1 total)
  • Thread Starter klol

    (@klol)

    I received a response from the developer of the module.
    Here is the code I used.

    add_filter( 'dgwt/wcas/search_query/args', function($args) {
    $user = wp_get_current_user();
    $allowed_roles = array( 'customer', 'subscriber');
    if ( array_intersect( $allowed_roles, $user->roles ) || !is_user_logged_in()) {
        $args['tax_query'][] = array(
          'taxonomy' => 'product_cat',
          'field'    => 'term_taxonomy_id',
          'terms'    => array(5709), // category IDs to be excluded for users that are not logged-in
          'operator' => 'NOT IN',
        );
      }
    
      return $args;
    } );
    
    add_action( 'pre_get_terms', function($query) {
      if (defined( 'DGWT_WCAS_AJAX' )) {
        $query->query_vars['um_ignore_exclude'] = true;
      }
    }, 10, 1 );
    • This reply was modified 2 years, 2 months ago by klol.
Viewing 1 replies (of 1 total)
  • The topic ‘Excludes products of a category to people not connected or Customer’ is closed to new replies.