Support

Account

Home Forums ACF PRO How to use Post Object to get product price?

Solving

How to use Post Object to get product price?

  • Hi, i am trying to get product price by using get_price_html() so that should display html with price, sale price or variation prices. My problem is: if i use this, the price which is showing is for variable product – the lowest variable sale price – it shows only sale price without regular price, for simple product everyhing is ok. How to get both regular price and sale price for variable product (for all variations price is the sime)?

    <?php if(get_field('bg_prods')) : ?>
    <div class="bg-prods__list">
    <?php $bg_list = get_field('bg_prods'); 
    foreach( $bg_list as $item):  $product = new WC_Product($item->ID);  ?>
    <div class="bg-prods__item">
    <a href="<?php echo get_permalink($item); ?>"><?php 
    echo '<figure>';
    echo get_the_post_thumbnail( $item, 'thumbnail' ); 
    echo '</figure>';
    echo '<h3>';
    echo get_the_title($item); 
    echo '</h3>';
    echo '<span class="prods__price">';
    echo $product->get_price_html(); 
    echo '</span>';
    ?></a>
    
    </div>
    <?php endforeach; ?>
    </div>
    <?php endif; ?> 
  • I checked that function $product = new WC_Product() is creating a simple product not with variation. How to make it right to create new product with variations?

  • ok, its working with

    $product = wc_get_product($item->ID);

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

You must be logged in to reply to this topic.