Home › Forums › Add-ons › Repeater Field › Determine if a Sub Field with a specific title is present
Hello!
I’ve created some custom Woocommerce tabs for the single product pages and am populating them based on the title of a repeater sub field. My ACF repeater field is “details_repeater” and have two subfields, “title” and “content”. I have an IF statement that checks to see if the subfield title matches a specific text string. In the example below “Characteristics” and if so, it echos the content. That all works fine. However, I would also like to an ELSE statement or something that checks to see if there isn’t a title subfield of “Characteristics” and then will do something. In this case, I want to add some CSS to hide the “Characteristics” WC Tab. I’m not sure how to check if a sub field with a specific title doesn’t exist. Any help would be greatly appreciated!
function woo_if_characteristics_tab_content()
{
echo '<h2>Characteristics</h2>';
while (have_rows('details_repeater')) {
the_row();
if (get_sub_field('title') == 'Characteristics') {
$content = get_sub_field('content');
echo $content;
} elseif (!get_sub_field('title') == 'Characteristics') {
echo 'does not exist';
}
}
}
I wonder if you need to use the woo product TAB callback.
So something like:
<?php
########################
# Add Custom Product Page TAB
########################
add_filter( 'woocommerce_product_tabs', 'acf_woo_new_product_tab' );
function acf_woo_new_product_tab( $tabs ) {
global $product;
$product_id = $product->get_id();
if( have_rows('details_repeater',$product_id ) ):
while( have_rows('details_repeater',$product_id ) ) : the_row();
if (get_sub_field('title') == 'Characteristics') :
// Adds the new tab
$tabs['product_details_tab'] = array(
'title' => __( 'Characteristics', 'woocommerce' ),
'priority' => 10,
'callback' => 'acf_product_details_tab_content'
);
endif;
endwhile;
endif;
return $tabs;
}
function acf_product_details_tab_content() {
global $product;
$product_id = $product->get_id();
// Check rows exists.
if( have_rows('details_repeater',$product_id ) ):
echo '<h2>Characteristics</h2>';
// Loop through rows.
while( have_rows('details_repeater',$product_id ) ) : the_row();
// Load sub field value.
$content = get_sub_field('content');
echo $content;
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;
}
Code is untested but hopefully, you can see what’s happening.
Hi, Jarvis
ahhh, yes. That’s a much better way to do it! Thanks for the help! Appreciate it!
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.