WordPress.org

Support

Support » Plugins and Hacks » WooCommerce » Get product price in custom loop

Get product price in custom loop

  • 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:

    <?php
    				$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'); ?>
    					</div>
    					<?php } else { ?>
    					<div class="post-box grid_4 <?php aero_post_box_class(); ?>">
    						<pre>
    						<?php var_dump($homepage_query); ?>
    						</pre>
    						<?php get_template_part('content', ''); ?>
    					</div>
    					<?php } ?>
    				<?php endwhile; ?>

    http://wordpress.org/extend/plugins/woocommerce/

Viewing 12 replies - 1 through 12 (of 12 total)
  • 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;

    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?

    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);

    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.

    any help would be very much appreciated.

    Thank you!

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

    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.

    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.

    Thanks @panstav, that’s a better way to implement it

    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.

    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

    @ravikathait01

    Maybe this will be what you are looking for:

    add_filter( 'woocommerce_get_price_html', 'function_name' );

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Get product price in custom loop’ is closed to new replies.
Skip to toolbar