Get the regular price of variable product in Woocommerce

纵饮孤独 提交于 2020-07-18 06:17:05

问题


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.



来源:https://stackoverflow.com/questions/52954139/get-the-regular-price-of-variable-product-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!