Support

Account

Home Forums Front-end Issues How do i display custom fields using Function.php

Solved

How do i display custom fields using Function.php

  • Hi I’ve got ACF working and displaying my custom fields inside a WooCommerce product page template using the following code:

    <?php
    		$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 want to display the fields and control how to position them on the front-end using the functions.php file. At the moment, the fields are showing above the product title, but need to be appearing below it. From what I can see, I need to get this done using the functions.php file.

    Can anyone help with this?

  • I think what you need to do is find the hook where you want these things displayed, have a look here:

    https://businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/

    I think the closest hook is woocommerce_before_add_to_cart_form.

    Try this in your functions.php file:

    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;
    }?>
  • That worked perfectly. Thanks for your help. That link to woocommerce hooks is really helpful too.

  • You’re very welcome, glad it worked out for you! 🙂

  • Hi,

    I have added this code to a dev site I’m working on is the code meant to show all the custom fields added to a particular product as it’s only showing the first one for me does anyone know why this is happening?

    Thanks,

    Jim

  • Hi,

    It’s OK I worked out the issue all is working fine.

    Jim

  • Works perfect! One more question, if i have 3 custom fields. How i can display them separately?

    One hook for one field, Another for another.
    Can you help?

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

The topic ‘How do i display custom fields using Function.php’ is closed to new replies.