Support

Account

Home Forums Front-end Issues Post object inside a repeater field

Solving

Post object inside a repeater field

  • I have a WooCommerce page where i am creating a new section similar to the “You may also like” or “Related products”, except this one will display specific products set on the product’s edit page.

    Right now, i have a Repeater field:

    • with one Post Object sub field
    • Filter by Post Type: Products
    • Return format: Post ID

    which is successfully showing the Product IDs of the three i set on the single-product edit page.
    Product  IDs from Post Object return format of Post ID

    What i need is to show / have access to is the post’s (Product’s) permalink, featured image and the other metadata. So do i need to be using the repeater with sub field Post Object with return format of Post Object? I actually am pretty sure I do but in the examples I didn’t see any featured_image where i did see returning permalink and title.

    My second part is, if you have any insight on what fields I might use to get the Product’s product details – price, add to cart, quick view values.

    There may be an overall easier way with other ACF fields, so i would love any insight on this.

    If it helps or matters, this code lives in the functions.php inside a add_action('woocommerce_after_single_product', 'new_section'); function.

    Thanks!

  • Maybe I should be using the code from Woocommerce’s template archive-product.php as that page displays products the same way I need to. So maybe i should try that code as a starting point and replacing the appropriate pieces of code with ACF fields (the three products chosen on the product’s edit page.)

    Wanted to add that idea which i am still looking into deeper.

  • Turns out – as usual with me – it was much simpler. I used a Woo shortcode that displays products (the way i wanted, with all the product details) and put it inside the loop that gets the PostID for the three products, set the PostID to a var, and it is almost working.

    It is working, except i need to adjust the loops syntax somehow:

     // check if the repeater field has rows of data
        if( have_rows('products_to_display_repeater') ):
    
            // loop through the rows of data
            while ( have_rows('products_to_display_repeater') ) : the_row();
    
                // vars
                $product_id = get_sub_field('product_complete_the_look');
                
                // get IDs from vars for Woo shortcode
                echo do_shortcode("[products ids='$product_id' limit='1' columns='1' orderby='date' order='desc']");
    
                // display a sub field value
                ?> 
                <?php the_sub_field('product_complete_the_look'); ?>
                <br>
                <?php 
    
            endwhile;
    
        else :
    
            // no rows found
    
        endif;

    The above works but because of echo do_shortcode("[products ids='$product_id' limit='1' columns='1' orderby='date' order='desc']"); I get three products each as 1 column and 1 row. I need one row with three columns.

    So the shortcode is being looped and called three times, once for each PostID. I need to only call the shortcode once and loop only the '$product_id' within the shortcode, thereby have three PostIds in the shortcode. Oh and they need to be comma separated.

    If anyone has input that’s great but I might figure it out…

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

The topic ‘Post object inside a repeater field’ is closed to new replies.