Support

Account

Home Forums Add-ons Repeater Field Woocommerce custom tab conditional

Solved

Woocommerce custom tab conditional

  • Trying to add conditional tabs to my product pages and have managed to get a text field to show but having issue getting the repeater field to display.

    Conditional tab: get_field / working

    /* Create Technical details tab */
    add_filter( 'woocommerce_product_tabs', 'specs_tab' );
    function specs_tab( $tabs ) {
      // ensure ACF is available
      if ( !function_exists( 'get_field' ) )
        return;
      $content = trim( get_field( 'technical_details' ) );
      if ( !empty( $content ) ) {
        $tabs[] = array(
          'title' => 'Technical specifications',
          'priority' => 15,
          'callback' => 'show_tech_content'
        );
      }
      return $tabs;
    }
    
    function show_tech_content() {
      echo get_field( 'technical_details' );
    }

    Conditional tab: have_rows / Not working

    add_filter( 'woocommerce_product_tabs', 'downloads_tab' );
    function downloads_tab( $tabs ) {
      // ensure ACF is available
      if ( !function_exists( 'have_rows' ) )
        return;
      $content = trim( the_sub_field( 'name_pdf' ) );
      if ( !empty( $content ) ) {
        $tabs[] = array(
          'title' => 'Downloads',
          'priority' => 15,
          'callback' => 'show_download_content'
        );
      }
      return $tabs;
    }
    
    function show_download_content() {
    if( have_rows('downloads') ):
     	// loop through the rows of data
        while ( have_rows('downloads') ) : the_row();
            // display a sub field value
            echo '<p>'.the_sub_field('name_pdf').'<.p>';
        endwhile;
    endif;
    }
  • Hi @renee

    Could you try this instead. I think it’s possible you could still use have_rows etc. for the looping of the repeater this just feels safer. Also I think your main issue was that you were looking if the_sub_field(‘product_pdf’) existed without first being in a loop (and the_* will always try to echo the results which is not desired when fetching a value for a variable).

    
    <?php
    add_filter( 'woocommerce_product_tabs', 'downloads_tab' );
    function downloads_tab( $tabs ) {
      // ensure ACF is available
      if ( !function_exists( 'have_rows' ) )
        return;
        
      if ( get_field('downloads') ) {
        $tabs[] = array(
          'title' => 'Downloads',
          'priority' => 15,
          'callback' => 'show_download_content'
        );
      }
      return $tabs;
    }
    
    function show_download_content() {
    	$downloads = get_field('downloads');
    	if( $downloads ):
    	 	// loop through the rows of data
    	    foreach ( $downloads as $download ) :
    	        // display a sub field value
    	        echo '<p>'. $download['name_pdf'].'<.p>';
    	    endwhile;
    	endif;
    }
    
  • Thanks Jonathon – that worked! Made my day 🙂

  • No problem glad I could help!

    Fell free to post in the forums whenever you have an issue with ACF and we’ll try to help you.

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

You must be logged in to reply to this topic.