4

I have a tab set to add a tab which contains specifications in WooCommerce. I'd like to wrap it into an if statement to only set the tab if the product is part of a certain category.

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );

function woo_custom_product_tabs( $tabs ) {

    global $post;

    if ($product->is_category("Mobile Phones")) { 
        $tabs['custom_specification'] = array( 'title' => __( 'Specification', 'woocommerce' ), 'priority' => 50, 'callback' => 'woo_custom_specification_content' );
    }
}

What is the right code to check the category for WooCommerce in the if statements brackets?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
mitchelangelo
  • 851
  • 4
  • 16
  • 42
  • I didn't forget I just didn't add it to save space I solved this problem about ten minutes after I posted the question will upload answer – mitchelangelo Jul 29 '16 at 21:05
  • Yes your's is kind of the same as my conclusion except I used ```if ($product->is_type( 'grouped' ) && has_term( 'SIM Cards', 'product_cat' ) || has_term( 'SIM Cards', 'product_cat' ) )``` – mitchelangelo Jul 29 '16 at 21:09
  • obviously after declaring ```$product = get_product( $post->ID );``` – mitchelangelo Jul 29 '16 at 21:10
  • I have the same sorta code for mobile phones my question was how to check product category the above is just an example of the same thing I did with SIM cards – mitchelangelo Jul 29 '16 at 21:12
  • ```has_term``` was what I was looking for thanks – mitchelangelo Jul 29 '16 at 21:14

2 Answers2

3

The conditional is_category() will return true if you are on a category archive page.

As you need a conditional for single product pages, you will target single product pages with is_product() conditional combined this way:

if ( is_product() && has_term( 'Mobile Phones', 'product_cat' ) ) {
    $tabs['custom_specification'] = array( 'title' => __( 'Specification', 'woocommerce' ), 'priority' => 50, 'callback' => 'woo_custom_specification_content' );
}

Or you could try also, in case, this one too:

if( is_product() && has_category( 'Mobile Phones' ) ) {
    $tabs['custom_specification'] = array( 'title' => __( 'Specification', 'woocommerce' ), 'priority' => 50, 'callback' => 'woo_custom_specification_content' );
}

@edit: You've missed return $tabs; at the end of your function before last closing bracket }.


References:

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
2

Try below code. This code add woocommerce tab only when product has Mobile Phones category.

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );

    function woo_custom_product_tabs( $tabs ) {

      global $post;
     if ( is_product() && has_term( 'Mobile Phones', 'product_cat' )) 
     { 
      $tabs['custom_specification'] = array( 'title' => __( 'Specification', 'woocommerce' ), 'priority' => 50, 'callback' => 'woo_custom_specification_content' );
      }
      return $tabs;
    }
pallavi
  • 359
  • 1
  • 4
  • Kindly, but you are just copying my answer! – LoicTheAztec Jul 28 '16 at 10:13
  • I correct the question code and add solution of issue . return $tabs; row is missing in question code. – pallavi Jul 28 '16 at 10:34
  • Yes but you are just copying my conditionals… so avoid this… thanks. The Op question is relative to conditionals. The `return $tabs;` is just an an oversight from the OP. You can add a comment in OP question for that… – LoicTheAztec Jul 28 '16 at 11:09