worisi24
worisi24

Reputation: 139

Get minimum price for simple and variable products in WooCommerce

I need to show a list of simple and variable products with their prices displayed as "From $XX". But I've seen some methods like below.

$min_price  = $product->get_variation_price( 'min', true ); //I guess this only works for variable products
$min_price = $product->get_price_html(); //Will this guarantee to show the priceof the cheapest variation in a variable product?
$min_price = $product->get_price(); //How about this?

So, which is the best way to ensure I always get the lowest price for products regardless of them being simple or variable.

Upvotes: 2

Views: 4231

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

The minimum price is exclusively for variable products (price range)

For variable products only to get the min active price amount (non formatted) you will use:

$min_price = $product->get_variation_price( 'min' );
$min_price_for display = $product->get_variation_price( 'min', true ); // for display

For other product types (including variations) to get the active price amount (non formatted) you will use:

$price = $product->get_price();

For all products to get the formatted displayed price, you will use:

$formatted_price = $product->get_price_html(); 

This last one can give a price range for variable products and for products on sale.


Related: WooCommerce variable products: keep only "min" price with a custom label

Upvotes: 3

Related Questions