phpwordpresswoocommerceproductrelated-content

Hide related product tab for specific products in Woocommerce


I saw this link: Unset product tabs for specific product categories in woocommerce

I want to hide related product tabs for specific products.

I use this code:

remove_action( 'woocommerce_after_single_product_summary', 'wpb_wrps_related_products',22 );
add_filter( 'woocommerce_product_tabs', 'wpb_wrps_adding_related_products_slider_to_product_tab' );
if( !function_exists('wpb_wrps_adding_related_products_slider_to_product_tab') ){
    function wpb_wrps_adding_related_products_slider_to_product_tab( $tabs ) {
        $tabs['wpb_wrps_related_products_slider'] = array(
            'title'       => __( 'Related Products','wpb-wrps' ),
            'priority'    => 30,
            'callback'    => 'wpb_wrps_related_products'
        );
        return $tabs;
    }
}

I used "unset( $tabs['related_products'] ); // (Related Products tab)" but there are the tab of related products for specific products.


Solution

  • In the code below, you will have to define an array of products where you want to hide the tab:

    remove_action( 'woocommerce_after_single_product_summary', 'wpb_wrps_related_products',22 );
    add_filter( 'woocommerce_product_tabs', 'wpb_wrps_adding_related_products_slider_to_product_tab' );
    if( !function_exists('wpb_wrps_adding_related_products_slider_to_product_tab') ){
        function wpb_wrps_adding_related_products_slider_to_product_tab( $tabs ) {
            global $product;
    
            // Define HERE the product IDs where you want to hide this custom tab
            $product_ids = array( 10, 15, 24, 98 );
    
            // If product match, we return normal tabs:
            if( in_array( $product->get_id(),  $product_ids ) ) return $tabs;
    
            // If product doesn't match, we add the custom tab:
            $tabs['wpb_wrps_related_products_slider'] = array(
                'title'       => __( 'Related Products','wpb-wrps' ),
                'priority'    => 30,
                'callback'    => 'wpb_wrps_related_products'
            );
            return $tabs;
        }
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested on Woocommerce 3+ and works