Support

Account

Home Forums Backend Issues (wp-admin) Snippet for Custom Field to Display in Product Tab Reply To: Snippet for Custom Field to Display in Product Tab

  • Hi @igeeksupport ,

    It is becuase our fields are not text but has options.
    Here is right code which worked for me.
    You can change EAN and Brand according to your fields names.

    /**
     * Add the custom tab
     */
    function my_simple_custom_product_tab( $tabs ) {
    		// Adds the new tab
    	if(get_field('ean')) //if there is custom field for this product, then show this tab
    	$tabs['my_custom_tab'] = array(
    		'title'    => __( 'Bran and EAN', 'textdomain' ), //You can change Brand and EAN as you wish
    		'callback' => 'my_simple_custom_tab_content',
    		'priority' => 50,
    	);
    
    	return $tabs;
    
    }
    add_filter( 'woocommerce_product_tabs', 'my_simple_custom_product_tab' );
    
    /**
     * Function that displays output for the tab.
     */
    function my_simple_custom_tab_content($slug, $tab ) {
    ?><h2><?php echo wp_kses_post( $tab['title'] ); ?></h2>
    <?php
    $brand= get_field_object('brand');
    $ean= get_field_object('ean');
    echo '<p>' . $brand['label']. ' : '. $brand['value'] .'</p>';
    echo '<p>' . $ean['label']. ' : '. $ean['value'] .'</p>';
    }