1

In WooCommerce I ma using "WPB WooCommerce Related Products Slider" and "Custom Related Products for WooCommerce" third party plugins.

With the code below I am adding a custom tab to display related products:

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

As some of my products don't have related products, how can I make this tab displayed just when there is related products?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
arz
  • 49
  • 1
  • 10

1 Answers1

0

Here is the way to get the related products count for the current product. With that information we can conditionally make your custom tab display or not based on that count:

if( !function_exists('wpb_wrps_adding_related_products_slider_to_product_tab') ){
    add_filter( 'woocommerce_product_tabs', 'wpb_wrps_adding_related_products_slider_to_product_tab' );
    function wpb_wrps_adding_related_products_slider_to_product_tab( $tabs ) {
        global $product;
        // Get the related products count
        $related_count = count( maybe_unserialize( get_option( '_transient_wc_related_'.$product->get_id() ) ) );
        // If no related products we exit
        if( empty( $related_count ) || $related_count == 0 ) return $tabs;

        $tabs['wpb_wrps_related_products_slider'] = array(
            'title'       => __( 'Related Products','wpb-wrps' ),
            'priority'    => 30,
            'callback'    => 'wpb_wrps_related_products'
        );
        return $tabs;
    }
    // Just for testing
    function wpb_wrps_related_products() {
        echo '<h3>HERE your custom related products loop (fake)</h3>';
    }
}

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello LoicTheAztec. I have an error: Fatal error: Cannot redeclare wpb_wrps_related_products() (previously declared in /../../public_html/wp-content/plugins/wpb-woocommerce-related-products-slider/inc/wpb-wrps-functions.php:20) in /../../public_html/wp-content/themes/clean-commerce/functions.php on line 49 – arz Sep 15 '17 at 07:25
  • I removed function wpb_wrps_related_products() and now I do not have fatal error but the output for related products is not good – arz Sep 15 '17 at 07:39
  • @arz This code is tested and perfectly works... Now what you need is to take inside my function what is useful for you: The important missing thing is to take the related products count code to use it in a conditional function, in your code… So you should accept this answer. That is the only things that I can do with the code provided… You should add the complete code as `wpb_wrps_related_products()` is missing… – LoicTheAztec Sep 15 '17 at 22:05
  • Hello. I added remove_action( 'woocommerce_after_single_product_summary', 'wpb_wrps_related_products',22 ) to the code. Now the appearance of the output is good but I see related products for that product which does not have any related products. – arz Sep 16 '17 at 07:55
  • @arz As this is specific to your additional plugins settings, **yes** this is the way for you (adding back the `remove_action`). The code I provide here is just for testing on a normal woocommerce website. The related products are saved in transient cache in `wp_options` table (for each product), where I am getting the data. Maybe you should try to clear/refresh them in woocommerce status > tools > clear transient and expired transient… Anyway, on a normal web site, my code works and doesn't display the custom tab when there is no related products. – LoicTheAztec Sep 17 '17 at 09:01