Support

Account

Home Forums Front-end Issues Displaying all fields in a group

Solving

Displaying all fields in a group

  • Sorry I am new the ACF and I am struggling. I have created a field group called: ‘product specifications’ and inside it contains 6 fields. All I want to do is get the field label and value displayed in a bullet point list but for the life of me I can’t figure it out.

    Seriously, ANY Help would be greatly appreciated.

  • Hi @angrypenguin88, I believe you can achieve this using the get_field_object() which will return the settings of a specific field.

    Here is the link to the same: get_field_object()

    When you get some time, could you please test it out and let me know how it goes?

  • Hi @kipmyk I was able to get the values to display with that command so thank you! it’s not pretty and I am sure there are better ways to do it but it works:

    add_action('woocommerce_before_add_to_cart_form', 'speaker_specification_fields', 4 );
    function speaker_specification_fields() { ?>
    
    <?php
    $field = get_field_object('field_6155c03d20d5c');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value']; ?>
    
    <?php
    $field = get_field_object('field_61712d2256ff2');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value']; ?>
    
    <?php
    $field = get_field_object('field_6155bf1660b3c');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value']; ?>
    
    <?php
    $field = get_field_object('field_60d47fd0711ca');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value']; ?>
    
    <?php
    $field = get_field_object('field_60d47ffb711cb');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value']; ?>
    
    <?php
    $field = get_field_object('field_6155bed832b78');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value'];
    }
    ?>

    I just call on every field individually.

  • Hi @angrypenguin88, glad you are making the progress. Kudos!

    Looking at the code implementation, that it is the best approach since there is no function you can call all the fields within from within the field group as far I can tell since you would need to supply the select which can either be a field key as you’ve done or field name.

    Please let me know if I can clarify more on the same!

    Happy to help!

  • Thank you, I really appreciate it. An issue I have come across is that if the product is marked as out of stock, it hides all the fields. Any idea how I can prevent this from happening?

    Thanks

  • Hi @angrypenguin88,

    Glad to help! Now regarding your second question. When you get some time, could you confirm if it only affects fields with products marked out of stock but it shows up within product not marked out of stock?

  • I suspect it’s to do with the filter on your function ‘woocommerce_before_add_to_cart_form’

    At a guess, if your product is out of stock, perhaps this isn’t triggered as the add to cart form won’t be displayed.

    Could be wrong!

    If you add the below to your function file, then check the message shows on both an instock and out of stock item:

    function custom_woocommerce_before_add_to_cart_form(){ 
       echo 'woocommerce_before_add_to_cart_form function is being called';
    } 
    add_action('woocommerce_before_add_to_cart_form', 'custom_woocommerce_before_add_to_cart_form');

    Worth trying to rule it out

  • @kipmyk Yes that’s correct. The fields show fine while the product is in stock but disappear when they are out of stock.


    @jarvis
    It displayed while in stock but not when it was out of stock.

    Thanks for your help on this, I really appreciate it.

  • Just tried this and confirm it’s the hook you’re using.

    If you switch to the below hook, you should see the message displayed whether an item is in or out of stock:

    add_filter('woocommerce_short_description','add_content_after_short_description');
    function add_content_after_short_description($description){
    	$text = "This will be displayed after the short des but before the add to basket";
    
    	return $description.$text;
    }

    So in theory, you can then start to add your fields back in:

    add_filter('woocommerce_short_description','add_content_after_short_description');
    function add_content_after_short_description($description){
    	#$text = "This will be displayed after the short des but before the add to basket"; #debug so remove
    
    	$field = get_field_object('field_6155c03d20d5c');	
    	$text = $field['label'].': '.$field['value'].'<br/>'; #first $text doesn't need a . before the =
    	
    	$field = get_field_object('field_61712d2256ff2');	
    	$text .= $field['label'].': '.$field['value'].'<br/>';	#note the . before the = as we wish to join/concate them
    	
    	$field = get_field_object('field_6155bf1660b3c');	
    	$text .= $field['label'].': '.$field['value'].'<br/>';	
    	# etc.
    
    	return $description.$text;
    }

    Not tested with the fields but definitely works with the correct hook!

  • @jarvis Worked perfectly! Thank you 🙂 However, it also displays the fields on the product overview page now also.

  • Hmm, what if you add a conditional to the function:

    <?php
    add_filter('woocommerce_short_description','add_content_after_short_description');
    function add_content_after_short_description($description){
    	
    
    	$field = get_field_object('field_6155c03d20d5c');	
    	$text = $field['label'].': '.$field['value'].'<br/>'; #first $text doesn't need a . before the =
    	
    	$field = get_field_object('field_61712d2256ff2');	
    	$text .= $field['label'].': '.$field['value'].'<br/>';	#note the . before the = as we wish to join/concate them
    	
    	$field = get_field_object('field_6155bf1660b3c');	
    	$text .= $field['label'].': '.$field['value'].'<br/>';	
    	# etc.
    	
    	if( is_product() ):
    		return $description.$text;
    	else:
    		return $description;
    	endif;
    }

    Does that work?

  • Hi @jarvis thanks for this. Unfortunately it didn’t seem to work. However, (and this probably isn’t the best solution) I was able to hide it on the product overview page using this css:

    /*-- HIDE HEADER PRODUCT OVERVIEW --*/
    					header.woocommerce-products-header {
        display: none;
    }

    So for now it is hidden and appears correctly on the product page when in and out of stock.

    Thanks.

  • Out of interest, if you change:
    is_product()
    To:
    is_product_category()

    Does that work?

  • Sorry, like this:

    
    	if( is_product_category() ):
    		return $description;
    	else:
    		return $description.$text;
    	endif;
    

    Otherwise it will be the wrong way round!

  • No that didn’t work either. I’ll just use the CSS option for now. Like I said, PHP is new to me so it could be that I have implemented the code wrong. My entire code is:

    // ACF SPECIFICATION FIELDS
    add_action('woocommerce_short_description', 'speaker_specification_fields', 4 );
    function speaker_specification_fields() { ?>
    	<ul>
    <li><?php
    $field = get_field_object('field_6155c03d20d5c');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
    
    <li><?php
    $field = get_field_object('field_61715110d855c');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
    	
    <li><?php
    $field = get_field_object('field_6155bf1660b3c');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
    
    <li><?php
    $field = get_field_object('field_60d47fd0711ca');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
    
    <li><?php
    $field = get_field_object('field_60d47ffb711cb');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
    
    <li><?php
    $field = get_field_object('field_6155bed832b78');
    ?>
    <?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
    	</ul>
    <?php
    	if( is_product_category() ):
    		return $description;
    	else:
    		return $description.$text;
    	endif; ?>
    <?php } ?>
Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.