How to get the price of variable product using variation ID?

user3619369 picture user3619369 · May 12, 2015 · Viewed 9.8k times · Source

I have variation ID of a product. Is there any way to get the price of particular variation ID.

I tried with the below code.

$variation_id = 12312;
$price = get_post_meta($variation_id, '_regular_price', true);

Answer

Raunak Gupta picture Raunak Gupta · Jan 1, 2017

You code will not display the correct price if you have set a sale price for that product so you should use _price key for that.

Here is the code which will workout for you.

$variation_id = '12312';
$price = get_post_meta($variation_id, '_price', true);

OR

$variation_id = '12312';
$variable_product = wc_get_product($variation_id);
//$regular_price = $variable_product->get_regular_price();
//$sale_price = $variable_product->get_sale_price();
$price = $variable_product->get_price();

Please Note: You can use anyone of the above given method but I'll recommend you to use the last one, because if WooCommerce change the metakey then the first code snippet will not work but the 2nd one will work.

Hope this helps!