Support

Account

Home Forums General Issues Relationship field in Woocommerce product loop

Solved

Relationship field in Woocommerce product loop

  • Hi,

    Looking to combine the default Woocommerce product loop with the relation field in ACF. Basically, I need the Woocommerce loop only to return products that are selected in the ACF relation field.

    The Woocommerce default product loop:

    
    <ul class="products">
    	<?php
    		$args = array(
    			'post_type' => 'product',
    			'posts_per_page' => 12
    			);
    		$loop = new WP_Query( $args );
    		if ( $loop->have_posts() ) {
    			while ( $loop->have_posts() ) : $loop->the_post();
    				wc_get_template_part( 'content', 'product' );
    			endwhile;
    		} else {
    			echo __( 'No products found' );
    		}
    		wp_reset_postdata();
    	?>
    </ul><!--/.products-->
    

    The relationship field I created in ACF has post type ‘Products’ selected, and is set to ‘Post object’.

    I tried a few things, but cant get it to work. Anyone has experience with this?

    Looking forward to your reply! Thanks.

  • Never mind, after posting the question I figured it out and it actually wasn’t difficult at all. See here below:

    
    <ul class="products">
    	<?php
        $ids = get_field('pp_relation', 'option', false, false);
    		$args = array(
    			'post_type' => 'product',
          'post__in' => $ids,
    			'posts_per_page' => 4,
    			'orderby' => 'rand'
    			);
    		$loop = new WP_Query( $args );
    		if ( $loop->have_posts() ) {
    			while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
    
            do_action( 'iclicks_related_products_loop' );
    
            echo '</div>';
    			endwhile;
    		}
    		wp_reset_postdata();
    	?>
    </ul><!--/.products-->
    
  • Hi, i want to do the same thing, but inside my post.
    to describe it more, here is what i have:

    within my post i can already return the price of the product that are selected in the ACF relation field (product_id).

    the code is:

    <?php 
        $price = get_field('product_id');
        if( $price ): ?>
          <?php foreach( $price as $p): ?>
    
                        <?php global $post;
                              $product = new WC_Product($p->ID);
                              echo wc_price( $product->price ); 
                         ?>
    
           <?php endforeach; ?>
        <?php endif; 
    ?>

    now i try the same thing, but i don’t want to output the price only, but the whole product instead.

    I tried to replace the code with something like this:

    <ul class="product">
    	<?php
                  $id = get_field('product_id');
                  $args = array(
    			'post_type' => 'product',
          'post__in' => $id
    			);
    	      $loop = new WP_Query( $args );
    		if ( $loop->have_posts() ) {
    			while ( $loop->have_posts() ) : $loop->the_post();
                              wc_get_template_part( 'content', 'product' );
                             
                              echo '</div>';
    			  endwhile;
    		       }
    		wp_reset_postdata();
    	?>
    </ul><!--/.product-->

    but it ofc displays all products from the post type ‘product’.

    how can i get the correct output, with the products that i’ve selected with the acf relationships field?

  • That’s great!

    But in this case, the custom order from the relationship field gets ignored.
    Is there a way to keep that?

    If I delete 'orderby' => 'rand' it will also ignore the custom order.

    EDIT: I found it: 'orderby' => 'post__in'

  • The solution with ACF relation field – post type ‘Products’ selected, and is set to ‘Post object’
    <ul class=”slider sp-interested-too-carousel product-carousel-boxes”>
    <?php
    $theProducts = get_field(‘sp_interested_too_products_choosing’);
    $theProductsIds = array();
    foreach ($theProducts as $product) {
    $theProductsIds[] = $product->ID;
    }
    $args = array(
    ‘post_type’ => ‘product’,
    ‘post__in’ => $theProductsIds,
    ‘posts_per_page’ => 10,
    ‘orderby’ => ‘rand’
    );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) : $loop->the_post();
    wc_get_template_part( ‘content’, ‘product’ );
    endwhile;
    } else {
    echo __( ‘No products found’ );
    }
    wp_reset_postdata();
    ?>

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

The topic ‘Relationship field in Woocommerce product loop’ is closed to new replies.