phpwordpresswoocommerceproducthook-woocommerce

Show and hide WooCommerce product tabs depending if user is logged in


I have four tabs for single products in woocommerce, I'll call them tabs A, B, C, D for simplicity sake.

When users are not logged in I need to show tabs "C" and "D", but hide tabs "A" and "B". When users are logged in, I need to show tabs "A" and "B" but hide tabs "C" and "D".

I have some php I've plugged into the functions child theme. I believe they are conflicting. I can get Tabs "A" and "B" to show for logged in customers, but then no tabs for not logged in customers. I can get Tabs "C" and "D" for not logged in customers, but then all four tabs show for logged in customers. Here are the snippets I'm using.

/**
 * hide product description and suggested retail tabs when not logged in
 */
add_filter( 'woocommerce_product_tabs', 'customize_product_tabs', 100 );
function customize_product_tabs( $tabs ) {

    if ( ! is_user_logged_in() ) { 
        unset( $tabs['description'] ); 
        unset( $tabs['suggested-retail'] );  
    
     // remove the description tab
    }
  return $tabs;
}
    
    /** 
 * Hides description-log-in and suggested-retail-log-in tabs  when logged in */

add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) { 
    
    if ($current_user->ID == '')  {
        unset( $tabs['description-log-in'] ); 
        unset( $tabs['suggested-retail-log-in'] );  
    } // remove the description tab

return $tabs;
}

I tried using one or the other with an "else" directive, but I usually get an error when saving or it results in the outcome of logged in users only see Tabs "A" and "B", but unlogged in users see nothing. The following is an example:

add_filter( 'woocommerce_product_tabs', 'customize_product_tabs', 100 );
function customize_product_tabs( $tabs ) {

    if ( ! is_user_logged_in() ) { 
        unset( $tabs['description'] ); 
        unset( $tabs['suggested-retail'] );  
   else 
        unset( $tabs['description-log-in'] ); 
    unset( $tabs['suggested-retail-log-in'] ); 
    
     // remove the description tab
    }
  return $tabs;
}

If I'm barking up an impossible tree, an alternative solution would be to only have tabs "A" and "B", with all the text visible for logged in users, but for non-logged in users, the tabs themselves need to still be visible, but the tab panels below where the detailed text is contained would need to be hidden.

Thank you for any assistance you are able to give.


Solution

  • There are some missing brackets in your code… Try the following instead:

    add_filter( 'woocommerce_product_tabs', 'customize_product_tabs', 100 );
    function customize_product_tabs( $tabs ) {
        if ( is_user_logged_in() ) { 
            unset( $tabs['description-log-in'] ); 
            unset( $tabs['suggested-retail-log-in'] );  
        } else { 
            unset( $tabs['description'] ); 
            unset( $tabs['suggested-retail'] );
        }
        return $tabs;
    }
    

    It should work.