Reece
Reece

Reputation: 2711

WooCommerce get sale price

I want to be able to show the current price and sale price if it exists within my WP_Query products loop. At the moment my code just replaces the current price with the sale price. I want an if statement that checks if the sale price is there or not. If it is I want to display both;

Here's my code;

<?php   
    $args = array(
        'post_type' => 'product',
        'meta_key' => 'total_sales',
        'orderby' => 'meta_value_num',
        'posts_per_page' => 4,
    );
    $query = new WP_Query( $args );
?>

<?php if($query->have_posts()): ?>
    <div class="container" id="best_sellers">
        <div class="row">
        <?php while( $query->have_posts() ): $query->the_post(); ?>
            <?php $product = wc_get_product(get_the_ID());?>

            <div class="col-md-3">
                <div class="cont">
                    <a href="<?php the_permalink(); ?>">
                        <div class="image_cont">
                            <?php the_post_thumbnail(); ?> 
                        </div>

                        <p class="sku"><?php echo $product->get_sku(); ?></p>

                        <p class="title"><?php the_title(); ?></p>
                    </a>

                    <div class="quantity">
                        <p>Quantity: </p>
                        <button class="increment">+</button>
                        <input type="text" value="1" min="1" max="<?php echo $product->get_stock_quantity(); ?>">
                        <button class="decrement">-</button>
                    </div>

                    <p class="price"><?php echo wc_price( wc_get_price_including_tax( $product ) ); ?></p>

                    <a href="<?php echo $product->add_to_cart_url();?>" data-quantity="1" class="basket_btn button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="<?php the_id(); ?>" data-product_sku="<?php echo $product->get_sku(); ?>" aria-label="Add “<?php the_title(); ?>” to your cart" rel="nofollow">Add to Basket</a>
                </div>
            </div>
        <?php endwhile; ?>
        </div>
    </div>
<?php endif; ?>

If you look at the price class you can see my code for getting the price.

update: I have been looking through the WooCommerce templates files and found this;

<?php if ( $product->is_on_sale() ) : ?>

This works, I just need to be able to grab both prices separately as at the moment

<?php echo wc_price( wc_get_price_including_tax( $product ) ); ?>

just gets overwritten by the sale price

Upvotes: 1

Views: 13569

Answers (3)

Rajeev Yadav
Rajeev Yadav

Reputation: 9

If you want to get the sale price with taxes, try this:

$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price()) );

You will have to prefix the currency which can be achieved via get_woocommerce_currency_symbol()

Upvotes: 0

Reece
Reece

Reputation: 2711

I managed to find this

<?php echo $product->get_price_html(); ?>

It does everything I wanted without having to make an if statement

Upvotes: 2

Adeel Nazar
Adeel Nazar

Reputation: 187

Do you mean regular and sale prices?

You can work with below code but this will only with simple product, it won't work on product variation.

global $product;
if( $product->is_on_sale() ) {
    $sale_price = $product->get_sale_price();
}
$regular_price = $product->get_regular_price();

Upvotes: 3

Related Questions