Support

Account

Home Forums Add-ons Repeater Field Nested repeater fields and conditional statements

Solved

Nested repeater fields and conditional statements

  • Hi,

    I’ve been playing around with this amazing plugin since yesterday and am trying to accomplish something with the Repeater add-on.

    What I have is a products database and each product has its own data. With ACF I can specify what regions it’s available in, the price in multiple currencies, where it can be ordered from etc. For the front end, I’d like to show a list of URLs where the product can be ordered from. I got this working, but I’d like to show a regional icon for each URL. Please note that my level of PHP knowledge is quite basic… I tried using get_field and such but no such luck.

    Here’s what I have set up:
    > Parent repeater field (product_urls)
    >> Child repeater fields (product_url_us, product_url_eu, etc. – each with website_name and product_url fields)

    How do I check if a specific product is part of a certain child repeater region so I can output the corresponding icon? I suppose a hidden field would be handy for this but alas (feature request?) 😛

    Thank you in advance!

  • Hi @eolis

    Could you please share the JSON or XML export of your field group?

    Thanks!

  • Hi @james, sure thing, I’ve added it as an attachment 🙂

  • Hi @eolis

    I think you can loop through the product_urls repeater and then check the keys like this:

    $productURLsRow = get_field('product_urls');
    print_r($productURLsRow);
    
    foreach($productURLsRow as $productURLs){
        foreach($productURLs as $key => $productURL){
            if(!empty($productURL)){
                if($key == 'product_urls_us'){
                    echo "us";
                } elseif ($key == 'product_urls_ca'){
                    echo "ca";
                }
            }
        }
    }

    I hope this helps.

  • Hi @james,

    Thank you! I tried the snippet you posted but it returns all the available keys for each row, instead of just the key corresponding with it. Let me explain: A single product I’m using for testing has six URLs “attached” to it: four for EU, one for AU and one for UK. Every URL, however, returns the values eu, uk and au instead of ‘eu’ for the European URLs, ‘au’ for the Australian URLs etc.

  • Hi @eolis

    Could you please provide me with some screenshots of the data on the backend, the result of the code I gave you, and the result you want?

    Thanks!

  • Hi @james,

    Sure, no problem! The first screenshot is of the data in the backend, the second is the output of the code you provided and the third is what I’m aiming for (it’s supposed to end up looking like a dropdown). Hope that helps!

  • Hi @eolis

    You need to modify it a little bit to make it like that. Something like this:

    $productURLsRow = get_field('product_urls');
    print_r($productURLsRow);
    
    foreach($productURLsRow as $productURLs){
        foreach($productURLs as $key => $productURL){
            if(!empty($productURL)){
                if($key == 'product_urls_us'){
                    $URLflag = 'us';
                } elseif ($key == 'product_urls_ca'){
                    $URLflag = 'ca';
                } elseif ($key == 'product_urls_eu'){
                    $URLflag = 'eu';
                } elseif ($key == 'product_urls_uk'){
                    $URLflag = 'uk';
                } elseif ($key == 'product_urls_au'){
                    $URLflag = 'au';
                } elseif ($key == 'product_urls_jp'){
                    $URLflag = 'jp';
                }
                
                foreach($productURL as $shopURL){
                    echo $URLflag . ' <a href="'. $shopURL["product_url"] .'">'. $shopURL["webshop_name"] .'</a>';
                }
            }
        }
    }

    Please learn more about Array and foreach function.

    I hope this helps.

  • Hi @james,

    Thank you, that’s exactly what I needed 🙂 I know I really need to brush up on my php, I don’t do it often enough I suppose. But thanks again for the help, much appreciated!

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

You must be logged in to reply to this topic.