Home › Forums › Front-end Issues › Displaying all ACF field labels and values from a specified field-group on WooC
I’m trying to display the contents of a specific ACF field group in a tab on a WooCommerce single product page. Unfortunately I’m not getting any data displayed using the code below, although I do if I list each field manually. Not an elegant solution and not future-proof with potential changes to the number of fields in the group. Is it also possible to specify the field group by name rather than ID for portability to other sites? Also no field should be displayed if there is no value.
/*==============================================================
* Create More Details tab
================================================================*/
if (class_exists('acf') && class_exists('WooCommerce')) {
add_filter('woocommerce_product_tabs', function($tabs1) {
global $post, $product; // Access to the current product or post
$custom_tab_title = '';
$tabs1['rpl-' . sanitize_title($custom_tab_title)] = [
'title' => 'More Details',
'callback' => 'rpl_custom_woocommerce_tabs',
'priority' => 10
];
return $tabs1;
});
function rpl_custom_woocommerce_tabs($key) {
global $post;
?><h2><?php echo $tab1['title']; ?></h2><?php
$specs_fields = get_specs_fields();
foreach ( $specs_fields as $name => $field ):
$value = $field['value'];
?>
<strong><?php echo $field['label']; ?>:</strong>
<?php echo $value; ?>
<?php endforeach;
}
function get_specs_fields() {
global $post;
$specs_group_id = 17; // Post ID of the specs field group.
$specs_fields = array();
$fields = acf_get_fields( $specs_group_id );
foreach ( $fields as $field ) {
$field_value = get_field( $field['name'] );
if ( $field_value && !empty( $field_value )) {
echo $field['value'];
$specs_fields[$field['name']] = $field;
$specs_fields[$field['name']]['value'] = $field_value;
}
}
}
return $specs_fields;
}
You cannot use get_field() to get the fields for a specific field group. You can only get the fields for a specific post. Data is saved against the post (product) where is created, not against the field group post for the group. $specs_group_id has not data and that is why you get not data.
There isn’t a way to get the fields for only a specific field group for the current post.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.