Get WooCommerce product variation price and sale price for your pricing table

So as usual I was peacefully working on a project of my own using WooCommerce as a base of my e-commerce solution. I had previous experience building standard shops consisting of multiple products with variable prices for each variation. But now I was creating a website with a single downloadable product that had multiple variations and each variation a custom price.

Now on the main product page I had to create a pricing table for this variable product to show off different pricing plans. And this is where I came across a problem of how to separately retrieve or get WooCommerce product variation price and sale price. I know that it sounds like an easy task and I assumed that WooCommerce definitely would have prepared such a function but I was mistaken.

In order to display custom product variation price I created a function with 2 attributes:

  1. $product_id – The ID of the variable product
  2. $variation_id – The ID of the variation that you need to get the price

So here is the function. You should add this in your functions.php file:

// Get Woocommerce variation price based on product ID
function get_variation_price_by_id($product_id, $variation_id){
	$currency_symbol = get_woocommerce_currency_symbol();
	$product = new WC_Product_Variable($product_id);
	$variations = $product->get_available_variations();
	$var_data = [];
	foreach ($variations as $variation) {
		if($variation['variation_id'] == $variation_id){
			$display_regular_price = $variation['display_regular_price'].'<span class="currency">'. $currency_symbol .'</span>';
			$display_price = $variation['display_price'].'<span class="currency">'. $currency_symbol .'</span>';

	//Check if Regular price is equal with Sale price (Display price)
	if ($display_regular_price == $display_price){
		$display_price = false;

	$priceArray = array(
		'display_regular_price' => $display_regular_price,
		'display_price' => $display_price
	$priceObject = (object)$priceArray;
	return $priceObject;

And after you have added the function you can easily use it to display WooCommerce product variation regular price an variation sale price on your website. Add this code where you want the prices to be displayed. Change 9 to your product ID and 15 to your product variation ID.

	$variation_price = get_variation_price_by_id(9, 15);
	echo $variation_price -> display_regular_price;
	echo $variation_price -> display_price;

If you are wondering where to get ID values of variations then you can take a look in the screenshot below. Product Variation ID values are right next to variation labels.

WooCommerce variable product variation ID value

Now you will be able to create custom pricing tables using Wordpres and WooCommerce and will not have to worry if you update product variation prices – they will be also updated and displayed in the pricing table. Please let me know your thoughts below and if you would like to get some extra information about the product variations.

In categories: Tutorials, Website development, Woocommerce, Wordpress

Nauris Kolāts

Nauris is a freelance designer / developer who loves to dig into the UX as much as in the ground for fishing worms. And fishing is just one amongst the long list of his active lifestyle hobbies.

Other posts

Your thoughts

Join the discussion

  1. Chris

    You’re a life saver man! Tried so much before I stumbled upon this. This is perfect :)

    • Great to hear that it helped :) Cheers!

  2. Kiara

    Thank you so much, it worked perfectly :D

  3. Joep van Dongen

    Great tutorial, only one question.
    Is it possible to make this line:
    variation_price = get_variation_price_by_id(9, 15);


    So Product ID and variation ID are filled out automaticly like:
    variation_price = get_variation_price_by_id($product->ID, $variation->ID);

    • Hey Joep,

      I think it is possible but I only required a very specific product to be customized like this.
      If you would like to do this using dynamic values, I am guessing you would have to define $product as global and then get an ID from it:
      global $product;
      $product_id = $product->get_id();

      And this is how you can get product variations:
      $available_variations = $product->get_available_variations();

      This is just a direction to go next, but hopefully it helps you.

  4. Learn Tutorial Point


    This post is good, very nice work

    Thank you

  5. Abdur Rahman

    Hi this is cool, im looking for help trying to show some prices for EMI plans on the pp.

    /*** start – show instalment price on product page***/
    add_action( ‘woocommerce_single_product_summary’, ‘show_inst_price’, 30 );
    function show_inst_price() {
    global $product;
    $product_id = $product->get_id();
    $product = wc_get_product( $product_id );
    if( $price > 20000 ){

    $sey36 = 2.849; // bank convenience ‘%’
    $multiplysey36 = $price * $sey36;
    $sey36price = $multiplysey36/$percentage;

    echo ‘0% interest instalments starting from Rs. ‘ . number_format($sey36price) . ‘/month‘;

    so this echos a calculation of $price, which is the lowest price of variation, i need $price to be dynamic, so when customer selects the variable price, his price will be updated on the div.


    • Hi Abdur,
      Thanks for your question. A bit hard to understand what exactly is going on in your code and therefore to help you out.

  6. Genki

    Will result in an error.

  7. Martin

    I have searched everywhere for a solution to getting the variation price output. This code doesn’t work for the newest version of WP + Woo. Could you please update the code? I am desperate to find a solution to my problem :) Cheers Martin

    • Hey Martin, thanks for getting in touch.
      I just tested the code and found there was a small issue in the code, now it’s been fixed and works with both with the latest WordPress and WooCommerce versions.

  8. Nauris

    Hey Nauris, thanks for this very clear article.
    Quick question: which link do you put for the add-to-cart buttons ? How does the system know that you are clicking on the “add-to-cart” for 25 euros ?

    • Hey Caroline,
      A very good question :)
      I guess the easiest way would be to simply manually ouptut an Add to Cart button under each link like so:
      <a href="">Add to Cart</a>
      You see there in the link number 9 represents product ID (make sure to change it according to your Product ID) and then simply make sure to add the correct variation ID value.

      Hope this helps.

  9. Marlon

    Hi Nauris!
    Thanks for sharing this solution.
    I have copied de php code on my functions.php page and try to put the code in a code section on Divi, but I got this:

    echo $variation_price -> display_price;

    What I have done wrong? Thank you.

    • Hi Marlon, not really sure how Divi code section works, but I would suggest checking if you have everything in place with opening and closing php tags . Maybe they are not required where you are using them.

  10. kenbo

    Hi Nauris,

    This is perfect and it worked for me !
    But there are a few things hope you suggest to me. I want to format currency.
    Ex: The price displayed is: 1234567$ and i want it display to: 1.234.567 $

    many thanks !

    • Hi Kenbo,

      This is how you can change the number formatting according to your needs:

      		$display_regular_price = preg_split("/display_regular_price, 2);
      		$display_regular_formatted_price =  number_format ( $display_regular_price[0] , 0 , "." , "." );
      		$formatted_display_regular_price_with_currency = $display_regular_formatted_price . ' <' . $display_regular_price[1];
      		echo $formatted_display_regular_price_with_currency;
      	//Preparing number format for display price
      		$display_price = preg_split("/display_price, 2);
      		$display_formatted_price =  number_format ( $display_price[0] , 0 , "." , "." );
      		$formatted_display_price_with_currency = $display_formatted_price . ' <' . $display_price[1];
      		echo $formatted_display_price_with_currency;
  11. metin

    I am in a very difficult situation. Please help me. The price of Variated products does not appear in the store. When I go to the product detail page and select the variation; The price does not change depending on the option. An error message appears when I press the add to cart button.
    Product with variations: Angora Ametis 2404A

    A lot of sites have shared code but it doesn’t work. The price does not change according to the variation. the variation does not trigger the price.

  12. Mike

    Nice, but your code do not show zero if price is for example: 24.90, then it shows only 24.9. There is no problem with 24.99 but if zero is last then you will see only 24.9 – do you have any idea how to fix that?

    • To always output 2 decimals, you can make small adjustments inside the get_variation_price_by_id() function.
      Replace this: $variation['display_regular_price'] with this: number_format( (float) $variation['display_regular_price'], 2, '.', '' );
      And replace this: $variation['display_price'] with: number_format( (float) $variation['display_price'], 2, '.', '' );

  13. esmail

    awesome, thank you very much for sharing this useful function :)

Latest work