Get the regular price of variable product in Wooco

2020-07-19 18:41发布

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条回答
贪生不怕死
2楼-- · 2020-07-19 19:25

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.

查看更多
登录 后发表回答