最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - How to display product price of the product in loop

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Feb 1, 2016 at 17:30 denis.stoyanov 1,2901 gold badge11 silver badges14 bronze badges asked Jan 25, 2016 at 21:49 yenneferyennefer 911 gold badge1 silver badge6 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 4

I sorted this out, $post should be $p in:

global $post;
$product = new WC_Product( $post->ID );

Example

<?php
global $woocommerce;
$currency = get_woocommerce_currency_symbol();
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
?>

<?php if($sale) : ?>
<p class="product-price-tickr"><del><?php echo $currency; echo $price; ?></del> <?php echo $currency; echo $sale; ?></p>    
<?php elseif($price) : ?>
<p class="product-price-tickr"><?php echo $currency; echo $price; ?></p>    
<?php endif; ?>

from this https://gist.github.com/aarifhsn/d0535a720d13369010ce

global $post;
$product = new WC_Product( $post->ID );
echo $product->get_price_html();

That will return the correct price for simple and variable products and should also return the currently active prices (regular / sale).

Edit: ref - https://woocommerce.github.io/code-reference/classes/WC-Product.html#method_get_price_html

发布评论

评论列表(0)

  1. 暂无评论