0

I am using a Repeater of Advance custom field for the content of the my additional custom WooCommerce Tab. The repeater is inside a group field. I manage to display the custom fields that is outside the repeater field. Now the problem is the field inside my repeater field. The repeater field is not displaying. Here is the code I used in my functions.php

// Add a Custom Tab

add_filter( 'woocommerce_product_tabs', 'dl_custom_product_designer_tab' );
function dl_custom_product_designer_tab( $tabs ) {
    // ensure ACF is available
    if ( !function_exists( 'have_rows' ) )
        return;

    if ( get_field('designer') ) {
        $tabs[] = array(
            'title' => 'DESIGNER',
            'priority' => 50,
            'callback' => 'dl_custom_designer_tab'
        );
    }
    return $tabs;
}

function dl_custom_designer_tab() {
    $designer = get_field('designer');
        echo '<p>'.$designer['designer_image'].'</p>';
        echo '<p>'.$designer['designer_name'].'</p>';
        echo '<p>'.$designer['designer_short_description'].'</p>';
        // loop through the rows of data
        $achievements = get_field('designer_achievements');
        if( $achievements ) {
            // loop through the rows of data
            echo '<ul>';
            foreach($achievements as $achievement){
                // display a sub field value
                echo '<li>'.$achievement['achievement'].'</li>';
            }
            echo '</ul>';
        }
}
5
  • 1
    is designer_achievements a field or a subfield?
    – CK MacLeod
    Commented Nov 18, 2017 at 8:40
  • can you show us a screenshot on how you created the field/subfiled ? Commented Nov 18, 2017 at 8:51
  • designer_achievement is a repeater type field then achievement is the subfield. Here is how I made the field. ibb.co/mGzvv6 Commented Nov 18, 2017 at 10:19
  • @CKMacLeod here is the screenshot Commented Nov 18, 2017 at 12:01
  • @TemaniAfif here is the screenshot Commented Nov 18, 2017 at 12:01

3 Answers 3

0

I believe you need to use get_sub_field() for the repeater field (designer_achievements) and get_field() for its parent. Please take a look at the ACF documentation for the Repeater field's subfield.

You will also need to use a have_rows() loop, as noted in the documentation.

if( have_rows('parent_field') ):

    while( have_rows('parent_field') ) : the_row();

        $value = get_sub_field('sub_field');

    endwhile;

endif;
2
  • Yes, I already tried that one. And sadly it is not working. I don't know if my logic is just wrong or I'm using the wrong syntax. Anyways thank you for the help. Commented Nov 18, 2017 at 10:12
  • I think you need to show your syntax. In the above example, designer_achievements would be the parent_field, and achievement would be the subfield.
    – CK MacLeod
    Commented Nov 19, 2017 at 8:54
0

replace $achievements = get_field('designer_achievements'); to $achievements = get_sub_field('designer_achievements');

1
  • Nah. Still no luck. I'll just used Wysiwyg Editor for now. Thanks for the help. Commented Nov 19, 2017 at 6:01
0

I search to do that and finally found a solution.You can do something like that:

This implies that your products have a repeater field "tabs" that contains a subfield "title" and a subfield "content".

add_filter( 'woocommerce_product_tabs', 'prefix_other_products_tab' );
function prefix_other_products_tab( $tabs ) {
    global $product;
    $additional_tabs = get_field('tabs', $product->get_id());
    foreach($additional_tabs as $index => $custom_tab){
        $slug = sanitize_title($custom_tab['title']);
        $tabs[$slug+$index] = array(
            'title' => $custom_tab['title'],
            'priority'  => 120,
            'content' => $custom_tab['content'],
            'callback'  => function($tab_name, $tab){
                global $product;
                echo $tab['content'];
            },
        );
    }
    return $tabs;
}

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.