How to set a tab (only) if the product is in a specific WooCommerce category

I have a tab to add a tab containing specifications in WooCommerce. I would like to include it in the if statement to set only 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 correct code for checking categories for WooCommerce in if statement brackets?

+4
source share
2 answers

The conditional is_category()will return true if you are on the categories archive page.

, is_product() :

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

, , :

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

@edit: return $tabs; }.


:

+3

. woocommerce , .

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;
    }
+2

Source: https://habr.com/ru/post/1649284/


All Articles