Skip to content

Instantly share code, notes, and snippets.

@justinstern
Created December 27, 2013 05:40
Show Gist options
  • Select an option

  • Save justinstern/8142999 to your computer and use it in GitHub Desktop.

Select an option

Save justinstern/8142999 to your computer and use it in GitHub Desktop.
Sample code for the WooCommerce Tab Manager plugin to conditionally display a tab based on the product category. To use this code, first go to WooCommerce > Tab Manager > Add Global Tab and create a new global tab, which for this example we'll name "Sample Tab". "Sample Tab" will now show up for every product that uses the global tab layout. Let…
<?php
add_filter( 'wc_tab_manager_product_tabs', 'wc_tab_manager_product_tabs' );
function wc_tab_manager_product_tabs( $tabs ) {
global $product;
foreach ( $tabs as $tab_name => $tab ) {
if ( 'my-tab' == $tab_name ) {
$categories = wp_get_post_terms( $product->id, 'product_cat', array( 'fields' => 'names' ) );
if ( ! in_array( 'My Category', $categories ) ) {
unset( $tabs[ $tab_name ] );
}
}
}
return $tabs;
}
@eskadesign
Copy link

Hi Justin, and thanks for sharing this snippet! I tried this code but had no luck. What I'm trying to achieve is to set the Global tabs to 1 category.

I've added this to my theme's functions.php
Here's my code:

add_filter( 'wc_tab_manager_product_tabs', 'wc_tab_manager_product_tabs' );
function wc_tab_manager_product_tabs( $tabs ) {
    global $product;
    foreach ( $tabs as $tab_name => $tab ) {
        if ( 'wapen-info' == $tab_name ) {
            $categories = wp_get_post_terms( $product->id, 'product_cat', array( 'fields' => 'names' ) );
            if ( ! in_array( 'jachtwapens', $categories ) ) {
                unset( $tabs[ $tab_name ] );
            }
        }
    }
    return $tabs;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment