Blog

2018.03.21

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

In categories: Tutorials, Website development, Woocommerce, Wordpress

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:

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.

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.

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.

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

Thoughts

  1. Chris

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

    • Nauris Kolāts

      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);

    dynamic?

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

    • Nauris Kolāts

      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

    Hello,

    This post is good, very nice work

    Thank you

 

Latest work