Support

Account

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

Solving

Snippet for Custom Field to Display in Product Tab

  • Hi, I created a new custom product tab. I would like to have the custom field I created to be placed in that custom tab. How do I do that? I am using a snippet plugin that will allow me to add in snippets so I don’t have to go through the main core files. Below is the coding for the add on custom product tab. Thanks

    add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
    function woo_new_product_tab( $tabs ) {
    	
    	// Adds the new tab
    	
    	$tabs['test_tab'] = array(
    		'title' 	=> __( 'Shipping', 'woocommerce' ),
    		'priority' 	=> 50,
    		'callback' 	=> 'woo_new_product_tab_content'
    	);
    
    	return $tabs;
    
    }
    function woo_new_product_tab_content() {
    
    	// The new tab content
    
    	echo '<h2>New Product Tab</h2>';
    	echo '<p>Here\'s your new product tab.</p>';
    	
    }
  • Hi @jzjzjzjzj,

    Thanks for the post.

    You will only need to call the ACF display function within the woo_new_product_tab_content() function.

    The code will look like so:

    add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
    function woo_new_product_tab( $tabs ) {
    	
    	// Adds the new tab
    	
    	$tabs['test_tab'] = array(
    		'title' 	=> __( 'Shipping', 'woocommerce' ),
    		'priority' 	=> 50,
    		'callback' 	=> 'woo_new_product_tab_content'
    	);
    
    	return $tabs;
    
    }
    function woo_new_product_tab_content() {
    
    	// The new tab content
    
    	echo '<h2>New Product Tab</h2>';
    	echo '<p>Here\'s your new product tab.</p>';
            echo get_field('fieldname', $post_id);
    	
    }
  • Hi James
    In this case, how shall we hide that tab or remove that tab if get_field('fieldname', $post_id); is empty.

    Sorry to bother you, I found the solution:

    //adding new tab//
    add_filter( 'woocommerce_product_tabs', 'product_tab' );
    function product_tab( $tabs ) {
    if ( get_field('fieldname', $post_id) ) {
    $tabs[] = array(
    'title' => 'Specifications',
    'priority' => 15,
    'callback' => 'show_content'
    );
    }
    return $tabs;
    }
    function show_content() {
    $downloads = get_field('fieldname', $post_id);
    if( $downloads ):
    echo get_field('fieldname', $post_id);
    endif;
    }

  • @debendra What if my field is in a repeater?

  • Thanks a lot, finding the tricks last 2days

  • I tried this snipped.
    I see only array wording in related tab.
    How i can fix it.

  • I AM ALSO FACING SAME ISSUE WITH MY SITE https://igeeksupport.org

  • 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>';
    }
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.