My general question is: how to display product price in woocommerce of a product in a loop, not the price of the product which page it is? In other words 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 product on the page for every single product in my grid – the price of the product which post it is, rather than the price of each product in a grid, if that makes sense… 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 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; ?>
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)
<php? }
Becasue 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 and everything works well now, apart the price.
Is there a way to target a price by ID
or sth
?