Home Forums WoodMart support forum Need help with custom tab

Need help with custom tab

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #33153

    MoeMauphie
    Participant

    Hi there,

    First off thank you very much for the recent update.

    I’ve added a new tab on product page to show a specific attribute there. I used the code snippet for the brand attribute which is available on the theme. I did manage to get it working but I have a small problem: When a product doesn’t have that specific attribute, although the tab is not shown, it still outputs html markup for that attribute which creates some extra gap between tabs.

    Here is the code that I have added to the function.php:

    /**
     * ------------------------------------------------------------------------------------------------
     * Add product tech tab to the single product page
     * ------------------------------------------------------------------------------------------------
     */
     
    add_filter( 'woocommerce_product_tabs', 'woodmart_product_tech_tab' );
    
    if( ! function_exists( 'woodmart_product_tech_tab' ) ) {
    	function woodmart_product_tech_tab( $tabs ) {
    		global $product;
    		$attribute = 'pa_tech';
    		$techs_attribute = $product->get_attribute( $attribute );
    		
    		if ( $techs_attribute ) {
    			$tabs['tech_tab'] = array(
    				'title' 	=> esc_html__( 'About Tech', 'woodmart' ),
    				'priority' 	=> 50,
    				'callback' 	=> 'woodmart_product_tech_tab_content'
    			);
    		}
    
    		return $tabs;
    	}
    }
    
    if( ! function_exists( 'woodmart_product_tech_tab_content' ) ) {
    	function woodmart_product_tech_tab_content() {
    		global $product;
    		$attr = "pa_tech";
    		if( ! $attr ) return;
    
    		$attributes = $product->get_attributes();
    
    		if( ! isset( $attributes[ $attr ] ) || empty( $attributes[ $attr ] ) ) return;
    
    		$techs = wc_get_product_terms( $product->get_id(), $attr, array( 'fields' => 'slugs' ) );
    
    		if( empty( $techs ) ) return;
    
    		foreach ($techs as $id => $slug) {
    			echo '<div class="woodmart-product-tech-description">';
    				$tech = get_term_by('slug', $slug, $attr);
    				echo do_shortcode( $tech->description );
    			echo '</div>';
    		}
    
    	}
    }

    I suppose I’m going to need some conditions there to prevent it forming outputting html when there’s no attribute. Since I’m not a coder I came here to seek you help. I’d really appreciate if you could help me with that.

    Thanks a lot,

    #33159

    MoeMauphie
    Participant

    Solved. It was because I was recording tabs.

    #33186

    Artem Temos
    Keymaster

    Great, we are glad that you have found the solution.

    #33233

    MoeMauphie
    Participant

    Solved. It was because I was recording tabs

    Of course I meant “reordering” not “recording” :-)) Bloody auto-correction!

    #33253

    Artem Temos
    Keymaster

    Yes, we got it 🙂

Viewing 5 posts - 1 through 5 (of 5 total)