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

11,595

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!

Share:
11,595

Related videos on Youtube

user3619369
Author by

user3619369

Updated on June 04, 2022

Comments

  • user3619369
    user3619369 almost 2 years

    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);
    
  • Ruyman21
    Ruyman21 almost 7 years
    This worked for me at that time, don't know why anybody would downvote this comment.
  • ban-geoengineering
    ban-geoengineering over 6 years
    Wouldn't changing the last line of your code to $price = $variable_product->get_price(); be even more future-proof?