Support

Account

Home Forums General Issues Trouble Conditionally Displaying Groups with Repeaters Based on Radio Buttons

Solved

Trouble Conditionally Displaying Groups with Repeaters Based on Radio Buttons

  • I’ve got a custom post type called Products. I’ve got an ACF field group that applies to that post type. Here’s what I’ve got for custom fields:

    Specifications (Group)
    – Product Type (Radio Button)
    – General Product Specs (Wysiwyg Editor)
    – Freestanding Wood Stove Specs (Group)
    – – [Repeater of text fields for each item]
    – Freestanding Gas Stove Specs (Group)
    – – [Repeater of text fields for each item]
    – Freestanding Pellet Stove Specs (Group)
    – – [Repeater of text fields for each item]
    – Wood Fireplace Specs (Group)
    – – [Repeater of text fields for each item]
    – Gas Fireplace Specs (Group)
    – – [Repeater of text fields for each item]
    – Gas Insert Specs (Group)
    – – [Repeater of text fields for each item]
    – Wood Insert Specs (Group)
    – – [Repeater of text fields for each item]
    – Hot Tub & Spa Specs (Group)
    – – [Repeater of text fields for each item]

    All those Specs groups have conditional logic, displaying depending on which Product Type radio button is checked.

    I have the ACF Them Code Pro plugin, so on the field group page, Theme Code has all the PHP for using all that. In my template, I’ve pasted that code and added some basic HTML. The problem is that it outputs everything, no matter what product type is selected. Here’s the code, but with just one product type to keep it short.

    <?php if ( have_rows( 'specifications' ) ) : ?>
      <?php while ( have_rows( 'specifications' ) ) : the_row(); ?>
        <?php if ( have_rows( 'freestanding_wood_stove_specs' ) ) : ?>
          <?php while ( have_rows( 'freestanding_wood_stove_specs' ) ) : the_row(); ?>
            <ul>
              <li><?php the_sub_field( 'heating_capacity_fws' ); ?> sq ft</li>
              <li><?php the_sub_field( 'emission_rate_fws' ); ?> grams per hr</li>
              <li><?php the_sub_field( 'efficiency_fws' ); ?>%</li>
              <li><?php the_sub_field( 'maximum_burn_time_fws' ); ?> hrs</li>
              <li><?php the_sub_field( 'btu_range_fws' ); ?></li>
              <li><?php the_sub_field( 'firebox_size_fws' ); ?> cubic ft</li>
              <li><?php the_sub_field( 'maximum_log_size_fws' ); ?>in</li>
              <li><?php the_sub_field( 'glass_area_fws' ); ?> sq in</li>
            </ul>
          <?php endwhile; ?>
        <?php endif; ?>
        (…The rest of the product types here…)
      <?php endwhile; ?>
    <?php endif; ?>

    So even if a different product type is selected and there’s nothing entered in the text fields for the other product types, they still all show up. So I’ve tried to add this conditional to check what radio is selected, but then nothing is output at all. Is the syntax correct for that?

    <?php if ( have_rows( 'specifications' ) ) : ?>
      <?php while ( have_rows( 'specifications' ) ) : the_row(); ?>    
        <?php if( get_field('product_type') == 'freestanding_wood_stove' ): ?>
            <?php if ( have_rows( 'freestanding_wood_stove_specs' ) ) : ?>
              <?php while ( have_rows( 'freestanding_wood_stove_specs' ) ) : the_row(); ?>
              <ul>
                <li><?php the_sub_field( 'heating_capacity_fws' ); ?> sq ft</li>
                <li><?php the_sub_field( 'emission_rate_fws' ); ?> grams per hr</li>
                <li><?php the_sub_field( 'efficiency_fws' ); ?>%</li>
                <li><?php the_sub_field( 'maximum_burn_time_fws' ); ?> hrs</li>
                <li><?php the_sub_field( 'btu_range_fws' ); ?></li>
                <li><?php the_sub_field( 'firebox_size_fws' ); ?> cubic ft</li>
                <li><?php the_sub_field( 'maximum_log_size_fws' ); ?>in</li>
                <li><?php the_sub_field( 'glass_area_fws' ); ?> sq in</li>
              </ul>
              <?php endwhile; ?>
            <?php endif; ?>
        <?php endif; ?>
      <?php endwhile; ?>
    <?php endif; ?>

    Anyone know what the overall issue is?

  • You’ll definitely need those conditionals in your PHP code.

    From what I can tell, it looks like you need change:
    get_field('product_type') to get_sub_field('product_type')

    Reason being, is it looks like from your structure, that “product_type” is a sub_field of the Specifications field (which is a “Group” field type), right? If so, that should do it.

  • I changed get_field('product_type') to get_sub_field('product_type'), but still no.

    Looking for any similar examples, I came across some that sparked a thought, that since the Return Value for the Radio Button is Both (Array), maybe I need to use variables like this, but it also didn’t work:

    <?php if ( have_rows( 'specifications' ) ) : ?>
      <?php while ( have_rows( 'specifications' ) ) : the_row(); ?>
        <?php 
          $product_type_array = get_sub_field( 'product_type' );
          $product_type_value = $product_type_array['value'];
          $product_type_label = $product_type_array['label']; ?>
        <?php if( get_sub_field($product_type_value) == 'fireplace_wood' ) { ?>
          <?php if ( have_rows( 'wood_fireplace_specs' ) ) : ?>
    	<?php while ( have_rows( 'wood_fireplace_specs' ) ) : the_row(); ?>
    	  <ul>
    	  <li><?php the_sub_field( 'heating_capacity_wf' ); ?> sq ft</li>
    	  <li><?php the_sub_field( 'maximum_burn_time_wf' ); ?> hrs</li>
    	  <li><?php the_sub_field( 'log_length_wf' ); ?>in</li>
    	  <li><?php the_sub_field( 'maximum_btus_wf' ); ?></li>
    	  <li><?php the_sub_field( 'emissions_wf' ); ?> grams per hr</li>
    	  <li><?php the_sub_field( 'efficiency_wf' ); ?>%</li>
    	  <li><?php the_sub_field( 'firebox_wf' ); ?> cubic ft</li>
    	  <li><?php the_sub_field( 'viewing_glass_wf' ); ?> sq in</li>
    	  </ul>
    	<?php endwhile; ?>
            <?php endif; ?>
        <?php } ?>
      <?php endwhile; ?>
    <?php endif; ?>

    I tried simplifying it to just test the conditional checking for Product Type, but this also doesn’t work.

    <?php if ( have_rows( 'specifications' ) ) : ?>
      <?php while ( have_rows( 'specifications' ) ) : the_row(); ?>
        <?php 
          $product_type_array = get_sub_field( 'product_type' );
          $product_type_value = $product_type_array['value'];
          $product_type_label = $product_type_array['label']; ?>
        <?php if( get_sub_field($product_type_value) == 'fireplace_wood' ) { ?>
          <p>Test</p>
        <?php } ?>
      <?php endwhile; ?>
    <?php endif; ?>

    So I’m still at a loss on what to do here.

  • Below $product_type_array = get_sub_field( 'product_type' ); put var_dump(get_sub_field( 'product_type' ); and let me know what it says.

    If it’s empty also try var_dump(get_field( 'product_type' )); and let me know.

  • Here’s what I get with var_dump(get_sub_field( 'product_type' ));

    array(2) { [“value”]=> string(14) “fireplace_wood” [“label”]=> string(16) “Fireplace – Wood” }

  • I see the problem now!! I missed it on your previous post.

    Ok, in your “Test” code above, you need to change:

    if( get_sub_field($product_type_value) == 'fireplace_wood' )

    to just:

    if( $product_type_value == 'fireplace_wood' )

    Because your $product_type_value is already storing the value from the sub_field. So you don’t pass that back into your sub_field.

    You should be good to go now!

  • @speakincode That did the trick! Thanks so much for your help, I appreciate it!

  • No problem. Glad you got it worked out.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Trouble Conditionally Displaying Groups with Repeaters Based on Radio Buttons’ is closed to new replies.