Support

Account

Home Forums Add-ons Repeater Field When using the repeater plugin, what should I do if there is no value?

Helping

When using the repeater plugin, what should I do if there is no value?

    • RAD

    • May 15, 2020 at 2:37 am

    Hi.
    I am trying to create an additional parts list for a product using the repeater plugin.
    I even configured to display the list, but I ran into a problem.

    It is configured to enter the price (number) and sale (text) using the repeater and sort the price in the lowest order.

    ex>
    order 1: auto parts 1(label) / price(the_sub_field[‘price’]) / discount rate (the_sub_field[‘sale’])
    order 2: auto parts 2(label) / price / discount rate
    order 3: auto parts 3(label) / price / discount rate

    	<?php
    	$fields = get_field_objects();
    	if($fields): ?>
    
    	<?php
        $fieldsWithValue = [];
        foreach ($fields as $field) {
            if ($field['value']) {
                $fieldsWithValue[] = $field;
            }
        }
        usort($fieldsWithValue, function($a, $b) {
            return $a['value'] <=> $b['value'];
        });
    	?>
    	
    	<div id="seller_list">
    		<h3>Price comparison</h3>
    		<ul>
    			<?php foreach( $fieldsWithValue as $field ): ?>
    			<li>
    				<div class="seller_name"><?php echo $field['label']; ?> :</div>
    				<?php while( have_rows($field['name'])): the_row(); ?>
    					<div class="seller_price">$<?php the_sub_field('price'); ?><span></span></div>
    					<div class="seller_sale"><?php the_sub_field('sale'); ?></div>
    				<?php endwhile; ?>
    			</li>
    			<?php endforeach; ?>
    		</ul>
    	</div>
    	<?php endif; ?>

    I checked the result values, but they all appear even if there is no value corresponding to the price.

    So what I want is to prevent the_sub_field (‘price’) from appearing in the list if it doesn’t exist, and how to apply number_format to the_sub_field (‘price’).

  • You cannot use have_rows() or get_sub_field() when you already have the array and you’ve removed the values in question. What you did with the array has no effect on what will be returned by ACF.

    You need to loop over the nested arrays.

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.