I am using code that displays the Related Products tab on a single product page.
// Display a multiselect field in "Linked Products" section add_action( 'woocommerce_product_options_related', 'display_handles_product_field' ); function display_handles_product_field() { global $product_object, $post; ?> <h2><strong>Related products in tabs</strong></h2> <p class="form-field"> <label for="handles_product"><?php _e( 'Related products', 'woocommerce' ); ?></label> <select class="wc-product-search" multiple="multiple" id="handles_product_ids" name="_handles_product_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>"> <?php $product_ids = $product_object->get_meta( '_handles_product_ids' ); foreach ( $product_ids as $product_id ) { $product = wc_get_product( $product_id ); if ( is_object( $product ) ) { echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>'; } } ?> </select> </p> <?php } // Save the values to the product add_action( 'woocommerce_admin_process_product_object', 'save_handles_product_field_value', 10, 1 ); function save_handles_product_field_value( $product ){ $data = isset( $_POST['_handles_product_ids'] ) ? array_map( 'intval', (array) $_POST['_handles_product_ids'] ) : array(); $product->update_meta_data( '_handles_product_ids', $data ); } /* New Handles Product Tab */ add_filter( 'woocommerce_product_tabs', 'new_handles_product_tab' ); function new_handles_product_tab( $tabs ) { /* Add new tab */ $tabs['new_handles_product_tab'] = array( 'title' => __( 'Related products', 'woocommerce' ), 'priority' => 50, 'callback' => 'new_handles_product_tab_content' ); return $tabs; } function new_handles_product_tab_content() { global $product; $product_ids = $product->get_meta( '_handles_product_ids' ); // Get handles if( ! empty($product_ids) ) $product_ids = implode(',', $product_ids); echo do_shortcode( "[products ids='$product_ids' columns='4']" ); // Using [products] shortcode for display. }
The code is working, but there is a small problem. If there are no manually added products in the “Related” tab, then all products of the store are displayed.
Tell me how you can fix this? How can I disable a tab if there are no added products in it?
I will be glad for your help!
Advertisement
Answer
You can use the same condition that you added in the new_handles_product_tab_content
function. Only add tab if products found.
/* New Handles Product Tab */ add_filter( 'woocommerce_product_tabs', 'new_handles_product_tab' ); function new_handles_product_tab( $tabs ) { global $product; $product_ids = $product->get_meta( '_handles_product_ids' ); // Get handles if( ! empty($product_ids) ){ /* Add new tab */ $tabs['new_handles_product_tab'] = array( 'title' => __( 'Related products', 'woocommerce' ), 'priority' => 50, 'callback' => 'new_handles_product_tab_content' ); } return $tabs; }