wordpress - How to display related product price on another single product page -


i display few related products on single product page, custom picked. using acf plugin relations field. trying code returns current product price, rather related products prices (in foreach):

<?php     $product = new wc_product(get_the_id());    echo wc_price($product->get_price_including_tax(1,$product->get_price())); ?> 

the code works fine on static pages, , prices few related products correct, not on single page product. thought part get_the_id() handles targeting of specific post?

update:

here whole code including acf relation field:

<?php      $posts = get_field('related_set_1');     if( $posts ): ?> <?php foreach( $posts $p): ?>     <li>         <a href="<?php echo get_permalink( $p->id ); ?>">             <?php                echo get_the_post_thumbnail( $p->id, '175x100' )                     ?>                 <div style="overflow:hidden">                     <h4><?php echo $p->post_title; ?></h4>                     <p class="price">                         <?php                          global $post;                         $product = new wc_product($post->id);                          echo wc_price($product->get_price_including_tax(1,$product->get_price()));                         ?>                     </p>                     <p class="link">view now</p>                 </div>         </a>     </li>     <?php endforeach; ?>         <?php endif; ?> 

i added global $post; reply below still have same output: price displayed price of product on page code placed, not product in 'related items' grid.

and use in functions.php in filter function, if makes difference?

add_filter( 'woocommerce_after_single_product_summary', 'custom_related_products' ); function custom_related_products() { ?> .... (the code above here) <php? } 

everything works well, apart price.

get_the_id() works when there wordpress loop. usage of wordpress loop depends on theme that's you're using.

for single post, it's highly wordpress loop isn't being used theme. more reliable way of doing calling global $post , accessing post id $post->id.

<?php  global $post; $product = new wc_product($post->id);  echo wc_price($product->get_price_including_tax(1,$product->get_price())); ?> 

Comments