0

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'];
    

    ?>
    :

} 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;

}`

1 Answer 1

0

I see what you're trying to achieve. You can use ACF's acf_get_field_group function with the title as a parameter to retrieve the field group and its ID.

Try this code:

/*==============================================================
Create More Details tab ================================================================*/ 
if (class_exists('acf') && class_exists('WooCommerce')) {
    add_filter('woocommerce_product_tabs', function($tabs1) {
        global $post, $product; 

        $tabs1['rpl-' . sanitize_title('More Details')] = [
            'title' => 'More Details',
            'callback' => 'rpl_custom_woocommerce_tabs',
            'priority' => 10
        ];

        return $tabs1;
    });
}

function rpl_custom_woocommerce_tabs($key) {
    global $post;

    echo '<h2>More Details</h2>';
    
    $specs_fields = get_specs_fields();

    foreach ( $specs_fields as $name => $field ):
        echo '<strong>' . $field['label'] . ':</strong> ' . $field['value'] . '<br>';
    endforeach;
}

function get_specs_fields() {
    global $post;

    $specs_group_title = 'Your Field Group Name Here'; // Change this to your ACF field group name
    
    // Fetching the group by title
    $group = acf_get_field_group($specs_group_title);
    if (!$group) return array();

    $specs_fields = array();

    $fields = acf_get_fields($group['key']);

    foreach ( $fields as $field ) {
        $field_value = get_field($field['key'], $post->ID);

        if (!empty($field_value)) {
            $field['value'] = $field_value;
            $specs_fields[$field['name']] = $field;
        }
    }

    return $specs_fields;
}

Make sure to change 'Your Field Group Name Here' to the name of your ACF field group. Then give that a try and see if it meets your requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.