-1

My goal is to add to WooCommerce /my-account page a select input type. The options of the select field will be controlled by ACF repeater in ACF options page, and will be inserted to wp_usermeta table.

I palce d this code in functions.php file.

    while ( have_rows('group_email') ) : the_row(); 
    ?>      
        <option>
    <?php
         echo the_sub_field('email_group_id'); 
    ?>
        </option>
    <?php

    endwhile;

For some reason i get no results. If I manually write options I can see them and it works great. I am using ACF version 4.4.11 The repeater is not empty.

What can be the problem?

2 Answers 2

1

The functions.php file doesn't know where you want that code to be shown on the front end. You need to put the code into a template page (ie the woocommerce account page) or attach it to that page with a woocommerce hook to the template.

You'll need to add it into a function to properly work if you decide it's better to leave it in your functions.php. In either case you may want to surround the while statement in an if statement in case there are no rows. Otherwise you'll get errors.

Lastly since you're echoing the field you should use

echo get_sub_field('email_group_id'); 

if you actually have more code than what you provided, and it's in the options page i would add the "option" argument to the field as well:

while ( have_rows('group_email' , 'option') ) : the_row(); 
?>      
    <option>
<?php
     echo get_sub_field('email_group_id', 'option'); 
?>
    </option>
<?php

endwhile;

It may be worth trying that first.

1
  • Great, Actually what made it woks was adding the option argument as you suggested. Thank you!
    – DavSev
    Mar 7, 2017 at 6:41
0

When You're not in The Loop you have to pass page ID as parameter to ACF functions.

while ( have_rows('group_email', $id) ) : the_row(); 
?>      
    <option>
<?php
     echo the_sub_field('email_group_id', $id); 
?>
    </option>
<?php

endwhile;

See documentation: https://www.advancedcustomfields.com/resources/have_rows/

2
  • If it is coming from the options page he shouldn't have to be in a loop.
    – rudtek
    Mar 7, 2017 at 6:39
  • Ohh....I thought it's coming from the page. My bad. Mar 7, 2017 at 6:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.