-->

Get the regular price of variable product in Wooco

2020-07-19 18:47发布

问题:

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

回答1:

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:

global $product;

// Regular price min and max
$min_regular_price = $product->get_variation_regular_price( 'min' );
$max_regular_price = $product->get_variation_regular_price( 'max' );

// Sale price min and max
$min_sale_price = $product->get_variation_sale_price( 'min' );
$max_sale_price = $product->get_variation_sale_price( 'max' );

// The active price min and max
$min_price = $product->get_variation_price( 'min' );
$max_price = $product->get_variation_price( 'max' );

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.