how to display product price in woocommerce of a product in a loop
-
I would like to display few related products in a grid on a single product page, but when I use this code :
<?php $product = new WC_Product(get_the_ID()); echo wc_price($product->get_price_including_tax(1,$product->get_price())); ?>
it displays price of a main single product which is on the page, rather than showing price for each related product in a loop, So if the price of the product on single page is £9.00, every product in related products grid will display with £9.00 too rather than it’s own price…
I am using ACF relations field to pick the products on a page.
Here is my whole code including ACF relation field:
<?php $posts = get_field('related_set_1'); if( $posts ): ?> <?php foreach( $posts as $p): ?> <li> <a>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; ?>
And I use this in functions.php in filter function, if that makes any difference?
add_filter( ‘woocommerce_after_single_product_summary’, ‘custom_related_products’ ); function custom_related_products() { ?> …. (the code above here)
Because I display it on another product page I had to use
get_the_post_thumbnail( $p->ID, '175x100' )
instead of
the_thumbnail
as otherwise I had the same issue with thumbnails, but by using get_the_post_thumbnail everything works well now, apart the price. Is there a way to use similar code for price?
- The topic ‘how to display product price in woocommerce of a product in a loop’ is closed to new replies.