skip to Main Content

First things first. I am NOT proficient in PHP but I can sometimes modify it to do what I want.

I have a WooCommerce site with thousands of products. These are broken down into multiple "Brands". These "Brands" come from an additional plugin called "Perfect Brands WooCommerce" but I have used the "Brand" name as a category as well so I can utilise it.

I found some code on StackOverflow to enable me to display a Tab based on the category of the product, which I then modified for the various categories. As you can see, anything in the "bisley-workwear" category will show the "additional_brand_info_tab" with the content from the "bisley-workwear". The same goes for the other two categories.

What I want to do is show more than one tab for a particular category. i.e. if the product category is "bisley-workwear" I want to show multiple tabs. These would change depending on the category.

Any help would be greatly appreciated.

// Conditional Brand Info Tabs

add_filter( 'woocommerce_product_tabs', 'custom_tabs' );
function custom_tabs( $tabs ) {
    // Adds the new tab
if ( has_term( 'bisley-workwear', 'product_cat' ) ) { // // Shows this tab if the product is in the "bisley-workwear" category (Use category slug).
$tabs['additional-brand-info'] = array(
'title' => __( 'Bisley Workwear', 'woocommerce' ),
'priority' => 50,
'callback' => 'additional_brand_info_tab'
);
}
 elseif ( has_term( 'as-colour', 'product_cat' ) ) { // Shows this tab if the product is in the "as-colour" category (Use category slug).
$tabs['additional-brand-info'] = array(
'title' => __( 'AS Colour', 'woocommerce' ),
'priority' => 50,
'callback' => 'additional_brand_info_tab'
);
}
  elseif ( has_term( 'syzmik-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "syzmik-workwear" category (Use category slug).
$tabs['additional-brand-info'] = array(
'title' => __( 'Sizmik Workwear', 'woocommerce' ),
'priority' => 50,
'callback' => 'additional_brand_info_tab'
);
} 
    return $tabs;
}
function additional_brand_info_tab() {
    // The new tab content
if ( has_term( 'bisley-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "bisley-workwear" category (Use category slug).
    echo 'This is Bisley Workwear';
    }
  elseif ( has_term( 'as-colour', 'product_cat' ) ) { // Shows this tab if the product is in the "as-colour" category (Use category slug).
    echo do_shortcode('This is AS Colour');
  }
  elseif ( has_term( 'syzmik-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "syzmik-workwear" category (Use category slug).
    echo ('This is Syzmik Workwear');
  }
}

I’m not really sure what to do after modifying the code as above. At the moment this works for displaying one tab based on a category. Multiple tabs would be great.

2

Answers


  1. Chosen as BEST ANSWER

    So, thanks to Snuffy's prompting, I have found a solution. I'm not sure if this is the correct syntax or whether it could be done better, but it works.

    This code will display two tabs if a product belongs to the "bisley-workwear" category:

    // Conditional Brand Info Tabs
    
    add_filter( 'woocommerce_product_tabs', 'custom_tabs' );
    function custom_tabs( $tabs ) {
    // Adds the new tab
    if ( has_term( 'bisley-workwear', 'product_cat' ) ) { // Shows this tab if the 
    product is in the "bisley-workwear" category (Use category slug).
    $tabs['brand-info'] = array(
    'title' => __( 'Bisley Workwear', 'woocommerce' ),
    'priority' => 50,
    'callback' => 'additional_brand_info_tab'
    );
    $tabs['brand-info-2'] = array(
    'title' => __( 'Bisley Workwear 2', 'woocommerce' ),
    'priority' => 50,
    'callback' => 'additional_brand_info2_tab'
    );
    }
     elseif ( has_term( 'as-colour', 'product_cat' ) ) { // Shows this tab if the         
    product is in the "as-colour" category (Use category slug).
    $tabs['brand-info'] = array(
    'title' => __( 'AS Colour', 'woocommerce' ),
    'priority' => 50,
    'callback' => 'additional_brand_info_tab'
    );
    }
      elseif ( has_term( 'syzmik-workwear', 'product_cat' ) ) { // Shows this tab if 
    the product is in the "syzmik-workwear" category (Use category slug).
    $tabs['brand-info'] = array(
    'title' => __( 'Sizmik Workwear', 'woocommerce' ),
    'priority' => 50,
    'callback' => 'additional_brand_info_tab'
    );
    } 
        return $tabs;
    }
    function additional_brand_info_tab() {
        // The new tab content
    if ( has_term( 'bisley-workwear', 'product_cat' ) ) { // Shows this tab if the 
    product is in the "bisley-workwear" category (Use category slug).
        echo 'This is Bisley Workwear';
        }
      elseif ( has_term( 'as-colour', 'product_cat' ) ) { // Shows this tab if the 
    product is in the "as-colour" category (Use category slug).
        echo ('This is AS Colour');
      }
      elseif ( has_term( 'syzmik-workwear', 'product_cat' ) ) { // Shows this tab if 
    the product is in the "syzmik-workwear" category (Use category slug).
        echo ('This is Syzmik Workwear');
      }
    }
    function additional_brand_info2_tab() {
        // The new tab content
    if ( has_term( 'bisley-workwear', 'product_cat' ) ) { // Shows this tab if the 
    product is in the "bisley-workwear" category (Use category slug).
        echo 'This is Bisley Workwear 2';
        }
      elseif ( has_term( 'as-colour', 'product_cat' ) ) { // Shows this tab if the 
    product is in the "as-colour" category (Use category slug).
        echo ('This is AS Colour');
      }
      elseif ( has_term( 'syzmik-workwear', 'product_cat' ) ) { // Shows this tab if 
    the product is in the "syzmik-workwear" category (Use category slug).
        echo ('This is Syzmik Workwear');
      }
    }
    

    Note the addition of:

    $tabs['brand-info-2'] = array(
    'title' => __( 'Bisley Workwear 2', 'woocommerce' ),
    'priority' => 50,
    'callback' => 'additional_brand_info2_tab'
    );
    }
    

    and

    function additional_brand_info2_tab() {
    // The new tab content
    if ( has_term( 'bisley-workwear', 'product_cat' ) ) { // Shows this tab if the 
    product is in the "bisley-workwear" category (Use category slug).
        echo 'This is Bisley Workwear 2';
        }
      elseif ( has_term( 'as-colour', 'product_cat' ) ) { // Shows this tab if the 
    product is in the "as-colour" category (Use category slug).
        echo ('This is AS Colour 2');
      }
      elseif ( has_term( 'syzmik-workwear', 'product_cat' ) ) { // Shows this tab if 
    the product is in the "syzmik-workwear" category (Use category slug).
        echo ('This is Syzmik Workwear 2');
      }
    }
    

  2. And the final working code which displays 2 tabs based on the category of the product:

    // Conditional Brand Info Tabs
    
    add_filter( 'woocommerce_product_tabs', 'custom_tabs' );
    function custom_tabs( $tabs ) {// Adds the new tab
        if ( has_term( 'bisley-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "bisley-workwear" category (Use category slug).
            $tabs['brand-info-1'] = array(
                'title' => __( 'Bisley Workwear', 'woocommerce' ),
                'priority' => 50,
                'callback' => 'additional_brand_info_tab'
            );
            $tabs['brand-info-2'] = array(
                'title' => __( 'Bisley Workwear 2', 'woocommerce' ),
                'priority' => 50,
                'callback' => 'additional_brand_info_2_tab'
            );
        }
        elseif ( has_term( 'as-colour', 'product_cat' ) ) { // Shows this tab if the product is in the "as-colour" category (Use category slug).
            $tabs['brand-info'] = array(
                'title' => __( 'AS Colour', 'woocommerce' ),
                'priority' => 50,
                'callback' => 'additional_brand_info_tab'
            );
            $tabs['brand-info-2'] = array(
                'title' => __( 'AS Colour 2', 'woocommerce' ),
                'priority' => 50,
                'callback' => 'additional_brand_info_2_tab'
            );
        }
        elseif ( has_term( 'syzmik-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "syzmik-workwear" category (Use category slug).
            $tabs['brand-info'] = array(
                'title' => __( 'Sizmik Workwear', 'woocommerce' ),
                'priority' => 50,
                'callback' => 'additional_brand_info_tab'
            );
            $tabs['brand-info-2'] = array(
                'title' => __( 'Sizmik Workwear 2', 'woocommerce' ),
                'priority' => 50,
                'callback' => 'additional_brand_info_2_tab'
            );
        } 
        return $tabs;
    }
    
    function additional_brand_info_tab() {  // The new tab content
        if ( has_term( 'bisley-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "bisley-workwear" category (Use category slug).
            echo 'This is Bisley Workwear';
        }
        elseif ( has_term( 'as-colour', 'product_cat' ) ) { // Shows this tab if the product is in the "as-colour" category (Use category slug).
            echo ('This is AS Colour');
        }
        elseif ( has_term( 'syzmik-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "syzmik-workwear" category (Use category slug).
            echo ('This is Syzmik Workwear');
        }
    }
    
    function additional_brand_info_2_tab() { // The new tab content
        if ( has_term( 'bisley-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "bisley-workwear" category (Use category slug).
            echo 'This is Bisley Workwear 2';
        }
        elseif ( has_term( 'as-colour', 'product_cat' ) ) { // Shows this tab if the product is in the "as-colour" category (Use category slug).
            echo ('This is AS Colour 2');
        }
        elseif ( has_term( 'syzmik-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "syzmik-workwear" category (Use category slug).
            echo ('This is Syzmik Workwear 2');
        }
    }'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search