How to display woocommerce sale price or regular price if there is no sale price

I'm using a woocommerce product addons plugin where I would like to display the price of a product in my dropdown section of the addons. Currently the code I have is this

    <?php
$loop = 0;
$current_value = isset( $_POST['addon-' . sanitize_title( $addon['field-name'] ) ] ) ? wc_clean( $_POST[ 'addon-' . sanitize_title( $addon['field-name'] ) ] ) : '';
global $product;
?>
<p class="form-row form-row-wide addon-wrap-<?php echo sanitize_title( $addon['field-name'] ); ?>">
    <select class="addon addon-select" name="addon-<?php echo sanitize_title( $addon['field-name'] ); ?>">

        <?php if ( ! isset( $addon['required'] ) ) : ?>
            <option value=""><?php _e('None', 'woocommerce-product-addons'); ?></option>
        <?php else : ?>
            <!--<option value=""><?php _e('Select an option...', 'woocommerce-product-addons'); ?></option>-->
        <?php endif; ?>

        <?php foreach ( $addon['options'] as $i => $option ) :
            $loop ++;
            $price = apply_filters( 'woocommerce_product_addons_option_price',
                $option['price'] > 0 ? ' + ' . wc_price( get_product_addon_price_for_display( $option['price'] ) ) . '' : '',
                $option,
                $i,
                'select'
            );
            ?>
            <option data-raw-price="<?php echo esc_attr( $option['price'] ); ?>" data-price="<?php echo get_product_addon_price_for_display( $option['price'] ); ?>" value="<?php echo sanitize_title( $option['label'] ) . '-' . $loop; ?>" <?php selected( $current_value, sanitize_title( $option['label'] ) . '-' . $loop ); ?>><?php echo wptexturize( $option['label'] . ' (' ); echo balanceTags($product->get_price_html()) . $price ?>)</option>
        <?php endforeach; ?>

    </select>
</p>

I'm using this echo

$product->get_price_html()

what this does though is display the $"sale price" $"price" but I just want to display just the sale price or just the product price if there is no sale price. Looking at the code below, how would I accomplish this?

Solutions

This article worked for me. you can also try this one.

If you want to get the regular price or sale price of a woocommerce product and you are getting nothing you need to know the following:

If the product has no variations you can get the regular price and sales price just like that:

Get the price of a simple procut

<?php
#the product must be instantiated above like $product = new WC_Product();
echo $product->regular_price;
echo $product->sale_price;
?>

If the product has variations you will get nothing if you will use the code above.

You need to get the variation product prices.

Get the price of a product with variations

#1 Get product variations
$product_variations = $product->get_available_variations();

#2 Get one variation id of a product
$variation_product_id = $product_variations [0]['variation_id'];

#3 Create the product object
$variation_product = new WC_Product_Variation( $variation_product_id );

#4 Use the variation product object to get the variation prices
echo $variation_product ->regular_price;
echo $variation_product ->sale_price;

That should be all.

Enjoy.

Source: http://www.w3bdeveloper.com/how-to/how-to-get-regular-price-of-a-product-in-wordpress-woocommerce/

Pretty simple. We'll write a custom function that will first be sure on that if the product is on sale or not. Then it'll return regular or sale price based on the sale condition it defined previously. So the function will be:

/**
 * Returns product price based on sales.
 * 
 * @return string
 */
function the_dramatist_price_show() {
    global $product;
    if( $product->is_on_sale() ) {
        return $product->get_sale_price();
    }
    return $product->get_regular_price();
}

Now call this function the_dramatist_price_show() instead of $product->get_price_html() . You'll get the price based on is on sale or not without currency symbol.

Hope that helps.

That's what works for me with Wordpress 5.1 and WooCommerce 3.5.5 :

                $price = get_post_meta( get_the_ID(), '_regular_price', true);
                $price_sale = get_post_meta( get_the_ID(), '_sale_price', true);
                if ($price_sale !== "") {
                    echo $price_sale;
                } else {
                    echo $price;
                }

Similar questions

WooCommerce - How to only show sale price if it is defined? Otherwise only show regular price
So, I have this code in my homepage for the featured products in WooCommerce, and it's working but the thing is I need them to hide the sale price if it's null or it's not defined. Here's what I got so far: I need a code that can check if there is a sale price and show: And if there isn't a sale price defined, only show: Anyone can help me out with...
Putting regular price and sale price on woocommerce variation dropdown menu
First time posting here so please bear with me. I am using the following code: This code puts a price next to each item in a variations menu. What I'd like to do is to have it also post a sale price and have the regular price be struck through (as in, here's the regular price and the sale price). I tried the following: And while that does add in a ...
WooCommerce - Show Regular and Sale Price for The Product Variation which has Flat Price
The scenario is Simple product, Variable product are woocommerce's two different product types. What I am trying to achieve is, there are some products which have over 50+ variations which have a different sale and regular prices. A price range for that product is fare. But there are also products which's variations have same regular and sale price...
How to add suffix to both regular price and on sale price in WooCommerce?
I want to add ,- after the sale price and after the normale price in WooCommerce. The function I am using right now is: This is working but it only adds after the sale price in WooCommerce. It should be added after the standard price as well. Here is the HTML how its added right now: Thanks for your time!
Get WooCommerce product regular price and sale price from a WP_Query
I run mass discounts in a specific category successfully (lets say -50% in category id 123) and everything runs smoothly in product grid, product page and orders. (Wordpress 5.7, Woocommerce: 5.1.0, tried various mass-discount plugins) However the problem is that when I try to print a list in an admin page through a small plugin I made, I can not g...
Display WooCommerce variation (regular or sale) price by ID
I am developing a webshop where customers can add and edit product themselves on the homepage. They can do this through advanced custom fields. It worked fine until I suddenly noticed that the sale price of a product was not shown. This is because it is a variation of the main product. I currently use this code to retrieve the advanced custum field...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .