I found a lot of answers, but none works for a variable products. I would like to display both, sales price and regular price for variable products.
global $product;
if( $product->is_on_sale() ) {
$sale_price = $product->get_sale_price();
}
$regular_price = $product->get_regular_price();
This is working, but only if it is a normal product. I think there must be a possibility even for product variations, but I don't find it.
Any help is appreciated
For prices in variable products you need to use different methods specific to
WC_Product_Variable
Class. A variable product is made of multiple product variations, so the prices of a variable product are its product variations prices.You can get the min or the max prices of a variable product like:
For a variable product, the method
is_on_sale()
will return true if one of it's product variations is on sale…You ca also use the method
get_variation_prices()
that will give you a multidimensional array of all the product variations prices (active, regular and sale prices) set in the variable product.