WordPress.org

Forums

[closed] If statement - If user is logged in AND user role is wholesaler. Woocommerce (4 posts)

  1. ralph88
    Member
    Posted 1 year ago #

    Hi there,

    I hope somebody can help me.

    I am trying to create two if statements to work together. " If user is logged in AND their user role is wholesale then execute nothing. Else remove certain category from shop page.

    I have got it to work with each if statement individually but need them to work together.

    I haven't very experience with using if statements etc so would appreciate anybody pointing out where I'm going wrong.

    Many thanks!

    Ralph

    add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
    function custom_pre_get_posts_query( $q ) {
    
      get_currentuserinfo();
      global $current_user;
    
    if (is_user_logged_in() ) {
    
          if ( $current_user->ID) {
    
        $user_roles = $current_user->roles;
        $user_role = array_shift($user_roles);
    
        if ($user_role == 'wholesale') {
    
        } else {
    
    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;
    if ( ! is_admin() ) {
    $q->set( 'tax_query', array(array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => array( 'trade' ), // Don't display products in the trade category on the shop page . For multiple category , separate it with comma.
    'operator' => 'NOT IN'
    )));
    }
    remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
    }
    }
    }
    }
  2. James Huff
    Volunteer Moderator
    Posted 1 year ago #

    I recommend asking at http://wordpress.org/support/plugin/woocommerce so the plugin's developers and support community can help you with this.

  3. ralph88
    Member
    Posted 1 year ago #

    Thanks, I will do.

  4. James Huff
    Volunteer Moderator
    Posted 1 year ago #

    You're welcome!

Topic Closed

This topic has been closed to new replies.

About this Topic