Woocommerce is a nice handy and most used plugin for providing eCommerce solution on WordPress platform. It has a lot of features and many more features are available through various plugins build for it. One of such cool features is the product variation and another one is sale price.
Product variation allows you to add different flavours of the same product. For example if you add a phone as a product and you have different variety of the same product like black coloured, white coloured etc instead of adding them as separate products you can add them as variety to the first product. Another good example would be T-shirts where you can add different sizes as the product variation.
Woocommerce also allows you to specify a sale price on a product. Using this feature you can display on your page the regular price striked out emphasizing on the sale price. Just like you see on the shop windows; old price was 500 and now it’s 300.
If you are coding something and want to display the product price you can do so simply by echoing $product->price in side your loop. This will output the regular price of the product if there is no sale price or else it will give the sale price. If you want to get both the regular and the sale price you can get that simply by using $product->regular_price and $product->sale_price. This is simple and good as long as there are no variations to the product. If however the product has variations and also has regular and sale prices then the regular_price and sale_price variables of $product object won’t give you anything. In that case you can get those prices for each product variation by the following simple way:
#Step 1: Get product varations
$available_variations = $product->get_available_variations();
#Step 2: Get product variation id
$variation_id=$available_variations['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
#Step 3: Create the variable product object
$variable_product1= new WC_Product_Variation( $variation_id );
#Step 4: You have the data. Have fun :)
$regular_price = $variable_product1 ->regular_price;
$sales_price = $variable_product1 ->sale_price;
I recently ran into this problem when on the shop page no price was getting displayed. Once you get the product variations you can loop through them to find highest sale price, lower regular price, percentage off etc depending upon what you want. Simple isn’t it?