Support

Account

Home Forums Front-end Issues Add custom fields to custom product tabs

Solved

Add custom fields to custom product tabs

  • Hello,

    I’ve managed to display my custom fields on the product pages using the following code:

    add_action('woocommerce_before_add_to_cart_form', 'display_product_fields' );
    function display_product_fields() {
    	$fields = get_field_objects();
    	if( $fields ): ?>
    		<ul>
    			<?php foreach( $fields as $field ): ?>
    				<li><?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
    			<?php endforeach; ?>
    		</ul>
    	<?php endif;
    }?>

    However, I would like to move the fields to a custom WooCommerce tab I have created. I have used this code, but I could be completely wrong and out-of-my-depth. May someone please assist me?

    add_action('specifications_custom_tab', 'display_product_fields' );
    function display_product_fields() {
    	$fields = get_field_objects();
    	if( $fields ): ?>
    		<ul>
    			<?php foreach( $fields as $field ): ?>
    				<li><?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
    			<?php endforeach; ?>
    		</ul>
    	<?php endif;
    }?>

    Thank you so much in advance! – Holly

  • I found this explanation of adding custom product tabs https://wisdmlabs.com/blog/add-custom-tab-woocommerce-product-page/

    What you have appears that is should work but you did not include code to add the custom tab.

  • My apologies – I didn’t include all of the code, just the portion that I thought was relevant.

    Yes, I had created a custom tab in my child’s function.php file:

    /**
     * Add a custom product data tab
     */
    add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
    function woo_new_product_tab( $tabs ) {
    	
    	// Adds the new tab
    	
    	$tabs['test_tab'] = array(
    		'title' 	=> __( 'specifications_custom_tab', 'woocommerce' ),
    		'priority' 	=> 50,
    		'callback' 	=> 'specifications_custom_tab'
    	);
    
    	return $tabs;
    
    }
    function specifications_custom_tab() {
    
    	// The new tab content
    
    	echo '<h2>New Product Tab</h2>';
    	echo '<p>Here\'s your new product tab.</p>';
    	
    }

    Then I added this code straight after:

    add_action('specifications_custom_tab', 'display_product_fields' );
    function display_product_fields() {
    	$fields = get_field_objects();
    	if( $fields ): ?>
    		<ul>
    			<?php foreach( $fields as $field ): ?>
    				<li><?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
    			<?php endforeach; ?>
    		</ul>
    	<?php endif;
    }?>

    The code to add custom product fields to my custom tab (or any tab for that matter) didn’t work. Am I doing something wrong?

    Thank you again for all your help!

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

You must be logged in to reply to this topic.