phpwordpresswoocommerceproduct

Get product prices to be displayed in WooCommerce 3


I want to display 8 products of category X in my home page

I use the following code to get the products:

    <div class="row">
        <?php  
            $args = array(
                'post_type'      => 'product',
                'posts_per_page' => 8,
                'product_cat'    => 'cw'
            );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
                global $product;
        ?>
        
        <div class="col-md-3">
            <div class="product">
                <?php echo woocommerce_get_product_thumbnail(); ?>
                <p class="name"><?php echo get_the_title(); ?></p>
                <p class="regular-price"></p>
                <p class="sale-price"></p>
                <a href="<?php echo get_permalink(); ?>" class="more">more info</a>
                <form class="cart" action="<?php echo get_permalink(); ?>" method="post" enctype='multipart/form-data' style="display:inline;">
                    <button type="submit" name="add-to-cart" value="45" class="order">buy</button>
                </form>
            </div>
        </div>

With this I can get products, but I don't know which method to use to get regular and sale price.


Solution

  • You can use, get_sale_price for sale price and get_regular_price for regular price

    $product->get_sale_price();
    
    $product->get_regular_price();