WooCommerce Custom Tab with ACF Repeater Field

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

Topic woocommerce-offtopic advanced-custom-fields php Wordpress

Category Web


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

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


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;

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.