Ready to get started?Download WordPress


WooCommerce - excelling eCommerce
Get product price in custom loop (13 posts)

  1. jopacic
    Posted 1 year ago #

    Hi all,
    I'm working on a theme that uses loop for displaying blog posts, and now instead I want to use this loop for displaying WooCommerce products. I added post_type='product', and it's listing the products just nicely, but now I have a problem to display product's price! Is there a way to do this? Here's the code:

    				$args = array(
    					//'posts_per_page' => '2',
    					'post_type' => 'product',
    					'paged' => get_query_var('paged')
    				$homepage_query = new WP_Query($args);
    			<?php //query_posts('posts_per_page=4&paged='.get_query_var('paged')); ?>
    			<?php if ( have_posts() ) : ?>
    				<?php while ( $homepage_query->have_posts() ) : $homepage_query->the_post(); ?>
    					<?php if($style == 'blog_style') { ?>
    					<div id="blog-style" class="post-box">
    						<?php get_template_part('content', 'blog'); ?>
    					<?php } else { ?>
    					<div class="post-box grid_4 <?php aero_post_box_class(); ?>">
    						<?php var_dump($homepage_query); ?>
    						<?php get_template_part('content', ''); ?>
    					<?php } ?>
    				<?php endwhile; ?>


  2. helgatheviking
    Posted 1 year ago #

    at it'st most basic the price is simply post meta

    $price = get_post_meta( get_the_ID(), '_regular_price');

    but you can also use the WooCommerce product class... should probably wrap it in some kind of class_exists() test just so that it won't break if WooCommerce isn't installed, but at the simplest iteration i think this should get the price

    $product = new WC_Product( get_the_ID() );
    $price = $product->price;
  3. jopacic
    Posted 1 year ago #

    Helga, you rock.

    I used first suggestion, and in that case $price is an Array, whose [0] index is the price.

    But when I try this on a product that's on sale, for example, it costed $3000 and now is $2000, $price[0] = 3000. How can I access the actual, discount price?

  4. helgatheviking
    Posted 1 year ago #

    Sorry, should use the last parameter of get_post_meta

    $price = get_post_meta( get_the_ID(), '_regular_price', true);

    Sale price, is just a different meta key. I think it is:

    $sale = get_post_meta( get_the_ID(), '_sale_price', true);
  5. cdwebmarketing
    Posted 1 year ago #

    This thread has helped me very much

    I am able to accomplish getting the regular / sale price of products but only for products without product options.

    My price is the same but I sell product that can be ordered in different sizes: small/medium/large

    So for those products with variations,

    $sale = get_post_meta( get_the_ID(), '_sale_price', true);
    is not getting me the sale price.

  6. cdwebmarketing
    Posted 1 year ago #

    any help would be very much appreciated.

    Thank you!

  7. delatdecatsini
    Posted 1 year ago #

    I have same problem as cdwebmarketing, products if have variations will not display the price. Please help us.

  8. helgatheviking
    Posted 1 year ago #

    Variations have a different id so your get_post_meta() approach won't work. Variations are technically child posts of the main product id. Hope that helps some.

  9. Stav Pan Geffen
    Posted 1 year ago #

    Was customizing woocommerce search results to display price as well, I have a very basic understanding of PHP so I mostly improvise and google around for answers.

    Found this thread to be very useful, got me started, though I was looking for a way to display prices even if the product has variations

    Found this on woocommerce-template.php
    <?php woocommerce_get_template( 'loop/price.php' ); ?>

    Added it to the search result template and that's it, works like a charm.

  10. Raymond Lopez Aleman
    Posted 1 year ago #

    Thanks @PanStav, that's a better way to implement it

  11. Daniel
    Posted 11 months ago #

    I know it's already a bit an old thread but I'll still post my solution I use to get the price. It might help someone.

    First, I set the global $product;

    Then check if the price is not empty:

    <?php if ( $price_html = $product->get_price_html() ) : ?>

    Then you can echo out the price within any divs or whatever you want.

    <?php echo $price_html; ?>
    <?php endif; ?>

    This will not only echo out the price but also the currency symbol and if the product is on sale it also echos out the sales price.

  12. ravikathait01
    Posted 11 months ago #

    how to edit $product->get_price_html() string which this function return.

    ex it return
    553chf / month for 3 months. for subscriptoion plan. i want to remove "for three month " from this string.

    Thanks Ravi

  13. Nick Young
    Posted 9 months ago #


    Maybe this will be what you are looking for:

    add_filter( 'woocommerce_get_price_html', 'function_name' );

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic